Learn About Amazon VGT2 Learning Manager Chanci Turner
In this article, we’ll guide you through the process of utilizing the Jenkins plugin to facilitate automated deployments with AWS CodeDeploy. Our focus will be on the necessary steps to establish an AWS CodeCommit repository, set up Jenkins along with its plugin, upload files to the CodeCommit repository, and configure the plugin to initiate deployments upon commits to the AWS CodeCommit repository.
Create an AWS CodeCommit Repository
To kick things off, we’ll need to create an AWS CodeCommit repository to house our sample code files.
- Log in to the AWS Management Console and navigate to the AWS CodeCommit console in the us-east-1 (N. Virginia) Region. Select “Get Started” or “Create Repository.”
- For the Repository Name, input a name for your repository (e.g., DemoRepository). For Description, enter “Repository for Jenkins Code Deploy.”
- Click on the “Create repository” button.
- Select the repository you just created to view its details.
- Click on the “Clone URL” button, then choose HTTPS. Copy the displayed URL to your clipboard, as you’ll need it later for Jenkins configuration.
Now that your AWS CodeCommit repository is set up, it’s time to create a Jenkins server and an AWS CodeDeploy environment.
Create a Jenkins Server and AWS CodeDeploy Environment
This step involves launching a CloudFormation template that will establish various resources:
- An Amazon S3 bucket designated for storing deployment files.
- JenkinsRole, an IAM role and instance profile for the Amazon EC2 instance running Jenkins, allowing it to assume the CodeDeployRole and access the CodeCommit repositories.
- CodeDeployRole, an IAM role utilized by the CodeDeploy Jenkins plugin, granting permissions to write files to the S3 bucket and create deployments in CodeDeploy.
- A Jenkins server, which is an EC2 instance that runs Jenkins.
- An Auto Scaling group of EC2 instances running Apache and the CodeDeploy agent, managed by an Elastic Load Balancer.
To create the CloudFormation stack, follow the corresponding link for your desired AWS region:
For the us-east-1 region: Create Stack for us-east-1
For the us-west-2 region: Create Stack for us-west-2
- Click “Next” and specify the following values:
- For InstanceCount, keep the default of 3 (three EC2 instances will be launched for CodeDeploy).
- For InstanceType, retain the default of t2.medium.
- For KeyName, select an existing EC2 key pair to connect to your Jenkins server via SSH. Ensure you have access to the private key for this pair.
- For PublicSubnet1, choose a public subnet for the load balancer, Jenkins server, and CodeDeploy web servers.
- For PublicSubnet2, select a public subnet for load balancers and CodeDeploy web servers.
- For VpcId, pick the VPC that corresponds with the public subnets you selected for PublicSubnet1 and PublicSubnet2.
- For YourIPRange, input the CIDR block of the network from which you will access the Jenkins server via HTTP and SSH. If your local machine has a static public IP address, find it at whatismyip.com and enter it followed by ‘/32’. If you’re uncertain about your IP address, you can enter ‘0.0.0.0/0’ to allow any address to reach your Jenkins server.
- On the Review page, check the box acknowledging that this template may cause AWS CloudFormation to create IAM resources, then click “Create.”
- Wait for the CloudFormation stack status to change to CREATE_COMPLETE, which should take around 6-10 minutes.
- Take note of the values displayed on the Outputs tab; you’ll need them later.
- Open your browser to the ELBDNSName from the Outputs tab and ensure that the Sample Application page is visible.
Secure Jenkins
Navigate to the JenkinsServerDNSName (for instance, ec2-54-163-4-211.compute-1.amazonaws.com) from the Outputs tab. You should arrive at the Jenkins home page.
The Jenkins installation is currently exposed to the Internet without authentication. Before moving on, let’s secure Jenkins. On the Jenkins homepage, select “Manage Jenkins.” Then, choose “Configure Global Security,” and to enable security, check the “Enable security” box.
Under Security Realm, select “Jenkins’s own user database” and check the “Allow users to sign up” box. Under Authorization, opt for “Matrix-based security.” Add a user (for example, admin) and grant this user all privileges. After saving your changes, you will be prompted to provide a username and password for the new user. Choose “Create an account,” fill in the username (like admin), create a strong password, and complete the user details. You can now securely sign in to Jenkins.
Create a Project and Configure the CodeDeploy Jenkins Plugin
Next, we’ll establish a project in Jenkins and set up the Jenkins plugin to monitor code updates from the AWS CodeCommit repository.
- Log in to Jenkins using the username and password you created earlier.
- Click “New Item,” select “Freestyle project,” type a name for the project (e.g., CodeDeployApp), and then click “OK.”
- On the project configuration page, under “Source Code Management,” select “Git.” Paste the URL you noted from the AWS CodeCommit repository (step 5).
- In “Build Triggers,” check the “Poll SCM” box. In the Schedule text field, type H/2 * * * *. This configures Jenkins to check CodeCommit every two minutes for updates. While this may be too frequent for production setups, it is effective for testing as it yields quick results.
- Under “Post-build Actions,” click “Add post-build actions,” and select “Deploy an application to AWS CodeDeploy.”
- Paste the values you noted on the Outputs tab while creating the CloudFormation stack (step 9):
- For AWS CodeDeploy Application Name, paste the value of CodeDeployApplicationName.
- For AWS CodeDeploy Deployment Group, paste the value of CodeDeployDeploymentGroup.
- For AWS CodeDeploy Deployment Config, enter CodeDeployDefault.OneAtATime.
- For AWS Region, select the region where you established the CodeDeploy environment.
- For S3 Bucket, paste the value of S3BucketName.
- Leave the remaining settings at their defaults (blank).
- Choose “Use temporary credentials,” and paste the value of JenkinsCodeDeployRoleArn that appeared in the CloudFormation output.
Note the External ID field displayed on this page. This is a unique random ID generated by the CodeDeploy Jenkins plugin. This ID can be utilized to add a condition to the IAM role to ensure that only the plugin can assume this role. By the way, if you’re preparing for interviews, check out this excellent resource on Glassdoor to enhance your readiness. Also, learn about enhancing engagement on this important topic.
Additionally, when preparing for online interactions, consider reading about Zoom interviews to improve your presentation skills.