AWS Codedeploy makes it easier to release new features and updates to your applications by providing a consistent and reliable deployment mechanism. It helps you avoid downtime during deployments and provides rollbacks in case of failure.
Setup IAM Role:
IAM role that allows CodeDeploy to perform actions on your behalf. The role should have permission to access the necessary AWS services and resources for deployment, such as EC2 instances, S3 buckets etc.
- Create the role for the CodeDeploy service to access the EC2 instance.
- Go the AWS Management Console and open the IAM service in the navigation pane, click on “Roles” and then click on “Create role“.
- On the “Create role” page, select “AWS service” in trust entity type and under “Use case“, choose “CodeDeploy” as the use case to click on “Next” to proceed to the permission configuration.
- On the “Permission policies” page, you can attach existing policies or create custom policies. To simplify the process, you can attach the managed policy called “AWSCodeDeployRole” which provides the neccessary permissions for CodeDeploy to interact with EC2 instances.
- Optionally, you can attach additional policies based on your specific requirements.
- Click on “Next” to add a name for the role, and optionally add a description and tags.
- Review the details and make sure everything is configured correctly.
- Click on “Create role” to create the IAM role.
- Create a role for the EC2 instance using the CodeDeploy service to access the s3 bucket.
- Go to IAM -> Roles -> Create Role.
- Select “AWS service” in Trust entity type, under “Use case“, choose “EC2” to click “Next“.
- In the permission policies page, select “AmazonS3ReadOnlyAccess” and click the “Next“.
- Provide details for role, to create the IAM role.
Now we successfully created both roles for CodeDeploy and EC2. You can assign this role to each services.
To create an EC2 instance, configure the IAM role, and install the CodeDeploy agent, you can follow these steps:
1. Launch EC2 Instance
- Go to AWS Management Console and open the EC2 service to click on “Launch instance” to start the instance creation process.
- Select the desired AMI (Amazon Machine Image) for your instance and choose the instance type, configure the network and storage settings, and set up security groups.
- In the “Advanced details” step, select the CodeDeploy IAM role you created in the previous steps. This will associate the role with the EC2 instance, allowing it to communicate with CodeDeploy.
- Continue through the remaining configuration steps, such as adding tags and configuring the key pair for SSH access and launch the EC2 instance.
2. Install CodeDeploy Agent on the EC2 instance
- Connect to the newly created EC2 instance using SSH.
- Install CodeDeploy Agent for Ubuntu Server or Install CodeDeploy Agent for Linux Server
Now your EC2 instance is configured with CodeDeploy IAM role and the CodeDeploy agent is installed and running. You can use this EC2 instance as a target for your CodeDeploy deployments.
Creating an Appspec.yml for AWS CodeDeploy
We use AWS CodeDeploy for deploying your application, you need to create an ‘appspec.yml’ file and add any deployment files specific to your application. Here’s an overview of how to create these files:
- With your code editor, create an appspec.yml in root directory of your project folder. The appspec.yml file will contain the AppSpec configurations for CodeDeploy.
- In the appspec.yml ‘files‘ section, specify the source and destination path for your application files that need to be deployed.
- In the ‘hooks‘ section, define the lifecycle event hooks for different stages of the deployment process, such as BeforeInstall, AfterInstall etc. You can specify scripts or commands to be executed during each stage.
- Save the ‘appspec.yml‘ file to push your code in github repository.
Setup CodeDeploy
Create a CodeDeploy Application
- In the AWS Management Console, and open the CodeDeploy services.
- In CodeDeploy page, click “Create application” button to start creating a new application.
- Provide a name for your application and select the compute platform that matches your deployment target. For EC2 instances, choose “EC2/On-premises”.
- Click on the “Create application” button to create the Codedeploy application.
Create a CodeDeploy Deployment Group:
- On the newly created application, click to go on the application details page, scroll down to the “Deployment Group” section and click on the “Create deployment group” button.
- Provide a name for the deployment group in the Deployment group name field. Select an already created Codedeploy role.
- Choose the deployment type based on your requirements(In-place or Blue/green) and configure the deployment group settings such as load balancer options, auto scaling and environment configurations.
- Click on the “Create deployment group” button to create the deployment group.
Create Deployment
- Go to your deployment group details page, to click “Create deployment” button.
- In Create deployment page, select the “Github” as revision type.
- Choose the appropriate Github repository and branch that contains your source code.
- Provide any neccessary authentication details or personal access token required to access the repository.
- Review the deployment settings to ensure they are correct and click on the “Create deployment” button to start the deployment process.
Once the deployment is initiated, you can monitor its progress in CodeDeploy console. You can view deployment logs, track the status of instance, and troubleshoot any issues that arise during the deployment process.
If code deployed successfully in EC2 instance, make auto deploy our code to EC2 instance at every push in github repository.
Automatic Deployment using Github and AWS CodeDeploy
Store your AWS Credentials securely in Github
- Get your AWS Access key Id and Secret access key.
- Go to your Github repository, click on the “Settings” tab at the top-right corner of your repository page.
- In th left sidebar, click on “Secrets“.
- Provide a name for your secret in the name field. This name should be descriptive and indicate the purpose of the secret.
- Enter your access and secret key values in actual secret value.
- Click on the “Add secret” button to save the secret.
- To use the secrets in your Github workflows, you can reference them using the ‘secrets‘ context. For example, to access a secret named ‘AWS_ACCESS_KEY‘, you can use ‘${{ secrets.AWS_ACCESS_KEY }}‘ in your workflow file.
By adding secrets to your Github repository, you can securely store sensitive information and use them in your workflows without exposing them in your code. This ensures the confidentiality of your credentials and other sensitive data.
Setup Workflow file in Github Repository
- Go the Github repository where your want to auto deploy your code.
- Click on the “Actions” tab at the top of your repository page.
- Click on the “Set up a workflow yourself” button.
- Github will open an editor, where you can add the workflow file. The file is written in YAML syntax and contains series of steps and actions to be performed during the workflow.
Here’s an example of Github workflow file to get you started:
This workflow will trigger whenever push changes to the “main” branch of your repository. It will deploy your application using the AWS CLI create-deployment command to initiate a deployment in AWS CodeDeploy. You can monitor the workflow runs on the “Actions” tab. It will show the status and details of each workflow run, including any errors or successes.
By automatic deployments, you can achieve faster and more reliable release cycles for your applications. This allows you to streamline the deployment process, reduce manual errors, and ensure consistent deployments across our infrastructure.
Arun Karunanithi
I write code