- eks cluster configure
- Buildspec.yml
- Kubernetes deployment file
- Iam policies and role
Aws pipeline
Go to Services -> Elastic Container Registry -> Create Repository Name: eks-devops-nginx Tag Immutability: Enable Scan On Push: Enable Click on Create Repository Make a note of Repository name
(or can use Github)
Create STS Assume IAM Role for CodeBuild to interact with AWS EKS
In an AWS CodePipeline, we are going to use AWS CodeBuild to deploy changes to our Kubernetes manifests. This requires an AWS IAM role capable of interacting with the EKS cluster. In this step, we are going to create an IAM role and add an inline policy EKS:Describe that we will use in the CodeBuild stage to interact with the EKS cluster via kubectl.
Use awscli to create policy -
TRUST="{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::${ACCOUNT_ID}:root" }, "Action": "sts:AssumeRole" } ] }"
echo $TRUST
aws iam create-role --role-name EksCodeBuildKubectlRole --assume-role-policy-document "$TRUST" --output text --query 'Role.Arn'
iam-eks-describe-policy echo '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "eks:Describe*", "Resource": "*" } ] }' > /tmp/iam-eks-describe-policy
Cat /tmp/iam-eks-describe-policy
aws iam put-role-policy --role-name EksCodeBuildKubectlRole --policy-name eks-describe --policy-document file:///tmp/iam-eks-describe-policy
arn:aws:iam::468085339621:role/EksCodeBuildKubectlRole
Update EKS Cluster aws-auth ConfigMap with new role created in previous step
kubectl get configmap aws-auth -o yaml -n kube-system
ROLE=" - rolearn: arn:aws:iam::468085339621:role/EksCodeBuildKubectlRole\n username: build\n groups:\n - system:masters"
kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/mapRoles: |/{print;print "$ROLE";next}1" > /tmp/aws-auth-patch.yml
//Or//
kubectl edit configmap aws-auth -o yaml -n kube-system
kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
kubectl get configmap aws-auth -o yaml -n kube-system
Review the buildspec.yml for Code Build & Environment Variables
AWS Code build configuration -
Update Code Build Role to have access 1.Ec2 container registery full access 2.S3 bucket access 3.Sts access
Go to Services IAM -> Policies -> Create Policy In Visual Editor Tab Service: STS Actions: Under Write - Select AssumeRole Resources: Specific Add ARN Specify ARN for Role: arn:aws:iam::180789647333:role/EksCodeBuildKubectlRole Click Add
For Role ARN, replace your account id here, refer step-07 environment variable EKS_KUBECTL_ROLE_ARN for more details
arn:aws:iam:::role/EksCodeBuildKubectlRole
Click on Review Policy Name: eks-codebuild-sts-assume-role Description: CodeBuild to interact with EKS cluster to perform changes Click on Create Policy
REPOSITORY_URI = 180789647333.dkr.ecr.us-east-1.amazonaws.com/eks-devops
EKS_KUBECTL_ROLE_ARN = arn:aws:iam::180789647333:role/EksCodeBuildKubectlRole
EKS_CLUSTER_NAME = eksdemo1