Coder Social home page Coder Social logo

stevenacoffman / eks-deployer-lambda Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 4.0 45 KB

Example AWS Lambda to interact with AWS EKS kubernetes cluster

License: Apache License 2.0

Go 29.43% Makefile 1.98% JavaScript 2.92% Shell 65.67%
aws aws-lambda aws-eks golang serverless serverless-framework aws-iam-authenticator

eks-deployer-lambda's Introduction

EKS Deployer Lambda

This lamdba will make a deployment into an EKS environment. It is intended for use in or after an AWS CodeBuild or AWS CodePipeline job.

You will need to edit the serverless.yml file and config directory files to reflect your AWS account, subnets, etc.

Running the kubernetes_add_service_account_kubeconfig.sh script created rbac permissions (with cluster admin permissions!), and exports everything necessary for authentication to the cluster.

Why do I care?

The most interesting piece here is how authentication to the EKS kubernetes cluster is handled. EKS supports either IAM authN or a service account bearer token.

This repo is an example of using a kubernetes service account and exporting the bearer token out of the cluster

Alternatively, you could try using aws-iam-authenticator as a library you can use AWS IAM credentials to authenticate to an EKS Kubernetes cluster and receive a bearer token.

The second option (as in this example) was originally how this repo worked, but the first proved simpler and more robust.

If you want to go the other route, kubectl edit -n kube-system configmap/aws-auth and add the iam role of the lambda to the mapRoles section with a rolearn, username, and groups. However, using the aws-iam-authenticator as a library is also providing a temporary bearer token, and occasionally failed. The kubernetes rbac still needed to be manually applied. It just seemed complicated and slow. Maybe if you have a federated kubernetes this might be more worth pursuing.

Instead,

kubernetes_add_service_account_kubeconfig.sh

Three things are required for authentication:

  • Cluster Certificate Authority Data : aws eks describe-cluster --region us-east-1 --name $CLUSTERNAME --query cluster.certificateAuthority.data
  • Host : aws eks describe-cluster --region us-east-1 --name $CLUSTERNAME --query cluster.endpoint
  • Bearer Token : kubectl get secret "${SECRET_NAME}" --namespace "${NAMESPACE}" -o json | jq -r '.data["token"]' | base64 -D

Running this script will:

  • add serviceaccount
  • apply cluster admin rolebinding
  • export a kubeconfig wired up to authenticate using that service account's bearer token for testing
  • save the bearer token to AWS Parameter store for use by the lambda
./kubernetes-add-service-account-kubeconfig.sh

How could anyone else have made this project from scratch?

cd $GOPATH/src/github.com/ithaka/continuous-deployment/go/
export APPNAME="pullrequest-clone-pipeline"
mkdir $APPNAME
cd $APPNAME
echo "10.8.0" > .nvmrc
nvm install
npm init -f
npm install serverless --save-dev
npm install serverless-pseudo-parameters --save-dev
npx serverless create -t aws-go-dep --name $APPNAME

Getting Started

Use the AWS cli to add the parameters to SSM parameter store:

aws ssm put-parameter --name '/core/ithaka-cypress-github-otp' --value "$GITHUB_OAUTH_TOKEN" --type SecureString --region us-east-1
aws ssm put-parameter --name '/core/github-pr-webhook-secret' --value "$(ruby -rsecurerandom -e 'puts SecureRandom.hex(20)')" --type SecureString --region us-east-1

You will need Serverless framework version 1.22.0 or above.

Retrieve CloudFormation

aws cloudformation get-template --stack-name eks-deployer-lambda-test

./kubernetes_add_service_account_kubeconfig.sh eks-deployer-lambda default

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.