In this section we will be building our Amazon Machine Image Pipeline leveraging EC2 Image Builder service. EC2 Image Builder is a service that simplifies the creation, maintenance, validation, sharing, and deployment of Linux or Windows Server images for use with Amazon EC2 and on-premises. Using this service, eliminates the automation heavy lifting you have to build in order to streamline the build and management of your Amazon Machine Image.
Upon completion of this section we will have an Image builder pipeline that will be responsible for taking a golden AMI Image, and produce a newly patched Amazon Machine Image, ready to be deployed to our application cluster, replacing the outdated one.
In this section you have the option to build the pipeline manually using AWS console, or if you are keen to complete the lab quickly, you can simply deploy from the cloudformation template.
Download the template here.
To deploy from the command line, ensure that you have installed and configured AWS CLI with the appropriate credentials:
aws cloudformation create-stack --stack-name pattern3-pipeline \
--template-body file://pattern3-pipeline.yml \
--parameters ParameterKey=MasterAMI,ParameterValue=ami-0f96495a064477ffb \
ParameterKey=BaselineVpcStack,ParameterValue=pattern3-base \
--capabilities CAPABILITY_IAM \
--region ap-southeast-2
ami-0f96495a064477ffb
. If you choose to to use a different region, please change the AMI Id accordingly for your region.Download the template here.
If you need detailed instructions on how to deploy CloudFormation stacks from within the console, please follow this guide.
pattern3-pipeline
as the Stack Name.pattern3-base
as default ) as the BaselineVpcStack parameter value.ami-0f96495a064477ffb
if you choose to to use a different region, please change the AMI Id accordingly for your region. )When the CloudFormation template deployment is completed, note the output produced by the stack.
You can do this by clicking on the stack name you just created, and select the ‘Outputs Tab’ as shown in diagram below.
Please take note of the Pipeline ARN specified under Pattern3ImagePipeline output
In this section we will go through the process manually to get a better understanding of the how the pipeline is constructed in EC2 Image Builder service.
To build this pipeline there are several subtasks we need to do:
We are going to use an S3 bucket to store the the EC2 Image Build process, so lets create one.
As S3 is a global namespace, for consistency please use the naming convention pattern3-logging
with a unique UUID number at the end.
You can achieve this on a mac or UNIX terminal by setting a variable called $bucket as follows:
bucket=pattern3-logging-`uuidgen | awk -F- '{print tolower($1$2$3)}'`
echo $bucket
Hopefully you should have a bucket name returned to you which you can then use to create the bucket as follows:
aws s3 mb s3://$bucket --region ap-southeast-2
We will need to create an IAM role that will be used by the EC2 Image Builder service.This IAM role will be used as the instance profile role of the temporary EC2 instance the service will launch. The service will use this instance to run the necessary activity, in this case our patch update. Therefore the role will need to have the appropriate policies to do this activity.
Follow below steps to create the IAM role:
Navigate to IAM within the console and select ‘role’ from the left hand panel and then select ‘create role’ as shown:
Select ‘AWS Service’ from the types of trusted entities and then select ‘EC2’, and ‘next: Permissions’ as shown:
Using the filter, search & select the following policies: * EC2InstanceProfileForImageBuilder * AmazonSSMManagedInstanceCore
Click ‘Next:Tags’.
On the next screen click ‘Next:Review’.
Enter pattern3-recipe-instance-role
for the Role Name and add a description. The three policies listed above should be added as follows:
In the IAM console, locate the role you just created.
Click on the role and click + Add inline policy
Select the JSON Tab and paste in below policy, replace the <s3 logging bucket>
in the json snippet below with the bucket name you created in previous step.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::<s3 logging bucket>/*"
]
}
]
}
Click Review Policy
Enter a name for the policy, and click ‘Create Policy’
Once you are done with this, you should now see another entry in the Policies with the name you just specified, expanding on that you should see the policy specified as screen shot below.
Our EC2 Image Build pipeline is also going to need a security group that will be assigned to the temporary EC2 instance it uses, so lets create one now so that we can include it later in the lab.
Follow this guide to create a Security Group.
For this purpose, we do not need to assign anything in the Inbound rule of the security group.
We do need to ensure that the outbound rules allow traffic out to the internet.
Your Security group rules should look like below, so edit your security group accordingly:
Ensure that the security group is created in the VPC id you’ve taken note of in section 1.2.
If you don’t remember the VPC-id, please refer to the instruction on section 1.2 in this lab for clarification.
Name the Security Group pattern3-pipeline-instance-security-group
In this section we will create a construct in EC2 Image Builder called a Component. This construct essentially contains instructions on what you would like to build into the AMI. For more information about EC2 Image builder Component, please refer to this guide.
To do this, Please follow below following steps:
Navigate to the EC2 Image Builder service from the console main page.
From the EC2 Image Builder service, select Components from the left hand menu and then select Create Component as shown here:
Add the following values to to the options, leaving the rest of the settings as default:
Once that’s done, select ‘Define document content’
Copy and paste in below definition document in the section under it.
name: ConfigureOS
schemaVersion: 1.0
phases:
- name: build
steps:
- name: UpdateOS
action: UpdateOS
Please Note that this definition is specified in YAML, so please ensure indentation is correct.
In this scenario, we have a very simple definition in our component, which is to run an UpdateOS action which will the packages in our OS. There are many other action activity you can specify in the component. For more information about EC2 Image Builder component, please refer to this guide
When you have completed these inputs, select Create Component to complete the component setup.
Next, we will create an Image Builder Recipe, which specifies the components, and other configuration we are going to define for our pipeline.
To do this, please complete the following steps:
Select Recipes from the left hand menu and then select Create Recipe.
Enter the following as configuration details:
Select Enter custom AMI ID and enter: the AMI ID for Amazon Linux 2 AMI (HVM) in your region:
( In Sydney region ami-0f96495a064477ffb
, please change the AMI Id accordingly if you use other region.)
Under Build components select Browse build components and then filter by Created by me to include the component which you created earlier ( pattern3-pipeline-ConfigureOSComponent
)/
Once you have entered all of the configuration details, select ‘Create Recipe’ at the bottom of the screen.
We will now create the Image Builder Pipeline to run our recipe.
To do this, please complete the following steps:
Remain in the Image Builder Recipe screen and use the tick box to select the recipe which you just created.
From the Actions menu, select Create pipeline from this recipe as shown here:
Enter the following information to configure the pipeline:
Once you have completed the above configuration, select Next at the bottom of the screen to go to the next configuation page.
Leave the rest empty and click Review.
Review the configuration is according to our specification above, and click Create Pipeline
Take note of the pipeline ARN, as we will need this for the next section.
Now that we have created all the construct, we can test the pipeline to ensure that it is working correctly. To do this select Run Pipeline from the Actions menu with the pipeline selected as shown here:
Once this is executed, you can observe the pipeline execution, and wait for the AMI to be built.
EC2 Image Builder pipeline will execute an SSM Automation Document in the background to orchestrate all the activities in building the AMI. If you go into your System Manager Automation document console, you should be able to see the execution running, and observe the activities in more detailed.
Please refer to this guide on how to view the Automation document execution details in your console.
You should be able to see an execution running under ImageBuilderBuildImageDocument document, which is the document used by EC2 Image builder to execute it’s activities.
Now that you have completed the deployment of the Image Builder Pipeline, move to section 4 of the lab whre we will use Systems Manager to build the automation stage of the architecture.
END OF SECTION 3