Wednesday, 4 December 2024

Essential AWS CLI Commands for Building a CI/CD Pipeline

The AWS CLI (Command Line Interface) is a powerful tool that simplifies interaction with AWS services. It’s particularly useful in automating tasks in CI/CD pipelines, enabling efficient deployment and management of your applications. Here’s a list of essential AWS CLI commands, explained with simplicity to help you build robust CI/CD workflows.

1. aws configure

Command:

aws configure

Sets up the AWS CLI by prompting for your AWS Access Key, Secret Key, region, and output format. It ensures the CLI can communicate with your AWS account.

2. aws s3 cp

Command:

aws s3 cp ./artifact.zip s3://my-bucket/artifacts/

Uploads a build artifact (e.g., a ZIP file) to an S3 bucket. This is often used to store application packages for deployment.

3. aws s3 sync

Command:

aws s3 sync ./local-directory s3://my-bucket/

Synchronizes a local directory with an S3 bucket. Useful for uploading or updating multiple files in your pipeline.

4. aws cloudformation deploy

Command:

aws cloudformation deploy --template-file template.yaml --stack-name my-stack --capabilities CAPABILITY_IAM

Deploys or updates a CloudFormation stack using the specified template. This automates infrastructure provisioning as part of your CI/CD pipeline.

5. aws cloudformation describe-stacks

Command:

aws cloudformation describe-stacks --stack-name my-stack

Fetches detailed information about a specific CloudFormation stack, including its status and outputs. Useful for monitoring deployments.

6. aws codebuild start-build

Command:

aws codebuild start-build --project-name my-build-project

Starts a build in AWS CodeBuild. Often used to trigger builds programmatically as part of CI workflows.

7. aws codepipeline start-pipeline-execution

Command:

aws codepipeline start-pipeline-execution --name my-pipeline

Manually starts a CodePipeline execution. Use this command to trigger pipelines directly from scripts.

8. aws ecs update-service

Command:

aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment

Forces a new deployment of an ECS service. This command ensures the latest task definition is deployed to the service.

9. aws ecr get-login-password

Command:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <ecr-repo-url>

Generates a login password for Amazon ECR and logs in to Docker. This is necessary for pushing images to ECR repositories.

10. aws ecr describe-repositories

Command:

aws ecr describe-repositories

Lists all ECR repositories in your account. Use this to verify your repository setup before pushing images.

11. aws ecr create-repository

Command:

aws ecr create-repository --repository-name my-app

Creates a new ECR repository. This is often the first step before pushing Docker images in a CI/CD pipeline.

12. aws lambda update-function-code

Command:

aws lambda update-function-code --function-name my-function --s3-bucket my-bucket --s3-key artifacts/my-function.zip

Updates the code of a Lambda function with a new deployment package stored in S3.

13. aws deploy create-deployment

Command:

aws deploy create-deployment --application-name my-app --deployment-group-name my-group --s3-location bucket=my-bucket,key=artifacts/my-app.zip,bundleType=zip

Creates a new deployment in AWS CodeDeploy. This command triggers the deployment process for your application.

14. aws iam create-role

Command:

aws iam create-role --role-name my-ci-role --assume-role-policy-document file://trust-policy.json

Creates an IAM role with the specified trust policy. This is often required for granting permissions to CI/CD services.

15. aws iam attach-role-policy

Command:

aws iam attach-role-policy --role-name my-ci-role --policy-arn arn:aws:iam::aws:policy/AdministratorAccess

Attaches a policy to an IAM role, granting the necessary permissions for CI/CD tasks.

16. aws elb describe-load-balancers

Command:

aws elb describe-load-balancers

Provides details about your Elastic Load Balancers. Use this command to ensure proper routing after deployments.

17. aws autoscaling update-auto-scaling-group

Command:

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-group --launch-configuration-name my-new-config

Updates an Auto Scaling group to use a new launch configuration. This is essential for rolling out updates in EC2-based pipelines.

18. aws deploy get-deployment

Command:

aws deploy get-deployment --deployment-id d-ABC12345

Retrieves the status of a specific deployment in CodeDeploy. Use this to monitor deployment progress.

19. aws events put-rule

Command:

aws events put-rule --name ci-trigger --schedule-expression "rate(1 hour)"

Creates a rule in Amazon EventBridge to trigger CI/CD workflows at scheduled intervals.

20. aws ssm send-command

Command:

aws ssm send-command --targets Key=instanceIds,Values=i-1234567890abcdef --document-name "AWS-RunShellScript" --parameters commands=["./deploy.sh"]

Executes commands on EC2 instances using AWS Systems Manager. This is helpful for running deployment scripts remotely.

The AWS CLI is a crucial tool for building and managing CI/CD pipelines, offering flexibility and automation across AWS services. From storing build artifacts to deploying applications and monitoring resources, these commands provide a solid foundation for efficient CI/CD workflows.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home