소소한 기록

Linux Academy - Automating ECS Deployments Using AWS Lambda 본문

DevOps/Serverless2

Linux Academy - Automating ECS Deployments Using AWS Lambda

John C Kim 2019. 9. 29. 20:47

-. 

 

1. Get IAM security key to execute commands from aws cli.

2. Create an ECR repository from aws console.

 

3. Create a docker image from local machine and push the image to ECR, following the push commands from ECR repository "view push commands".

 

 

Retrieve the login command to use to authenticate your Docker client to your registry.Use the AWS CLI:

$(aws ecr get-login --no-include-email --region us-east-1)

Note: If you receive an "Unknown options: --no-include-email" error when using the AWS CLI, ensure that you have the latest version installed. Learn more 

 

Build your Docker image using the following command. For information on building a Docker file from scratch see the instructions here . You can skip this step if your image is already built:

docker build -t hello .

 

After the build completes, tag your image so you can push the image to this repository:

docker tag hello:latest XXX.dkr.ecr.us-east-1.amazonaws.com/hello:latest

 

Run the following command to push this image to your newly created AWS repository:

docker push 605022739057.dkr.ecr.us-east-1.amazonaws.com/hello:latest

 

4. Create ECS cluster. Follow instruction with get started option on aws console.

 

5. Fill out the detail as per screenshot below. Choose custom as container definition and enter image repository address as per ECR's image address.

 

6. Fill out task definition and service definition as required.

 

 

7. Create lambda function from aws cli. Ensure right ARN is used for the command.

 

aws lambda create-function \
--function-name ECS \
--handler lambda_function.lambda_handler \
--memory-size 1024 \
--timeout 15 \
--runtime python3.6 \
--zip-file fileb:///home/user/lambda_function.zip \
--role arn:aws:iam::836184656977:role/lambda_exec_role_LA \
--environment Variables="{NAME=secondcontainer,IMAGE=URI,TASK_DEF=linuxtaskdef,CLUSTER=linuxcluster,SERVICE=linuxservice}"

8. Get the URI of the image location from ECR and paste into environment variable as shown below.

 

 

9. Set up Cloudtrail. Enter the required field as required. Once Cloudtrail is created, ensure you configure CloudWatch Logs in the created Trail config.

 

 

Ensure CloudWatch log is configured for the newly created Cloudtrail

10. Create CloudWatch event rule. Select ECR as event source and enter "PutImage" for AWS API Call via CloudTrail.

Select the lambda function created earlier as a target.

 

 

 

11. Update the docker image and perform new docker build for the image. Push updated docker image as per instruction earlier.

 

12. You can confirm ECS cluster has kicked off and built a new task based on the new docker image registered in ECR from Tasks section of the ECS cluster.