|
| 1 | + |
| 2 | +# Using the EC2 Image Builder Terraform Module to create an Linux AMI |
| 3 | + |
| 4 | +This example will use the EC2 Image Builder Terraform Module to create an Amazon Linux 2023 Custom AMI. |
| 5 | + |
| 6 | +The build components are currently all in a single file: linuxbuild.yaml |
| 7 | +The tests components are currently all in a single file: linuxtest.yaml |
| 8 | + |
| 9 | +However the module supports providing multiple files, given the parameters build_component_arn and test_component_arn are of list of strings type. |
| 10 | + |
| 11 | +The module cover the creation of the core components of the EC2 Image Builder, like Image Pipeline, Image Recipe, Distribution Configuration, |
| 12 | +Infrastructure Configuration, etc, but there are components which needs to be created outside of the module and provide as input, which are basically |
| 13 | +what it is going to be executed during the build and test process, and also custom IAM Policy with permissions required to interact with |
| 14 | +AWS services related to your build and test components. |
| 15 | + |
| 16 | +This example creates the following resources: |
| 17 | + |
| 18 | +- S3 Bucket |
| 19 | +- EC2 Image Builder Build Component, which executes the hello world powershell script |
| 20 | +- EC2 Image Builder Test Component, which executes the pester script which tests the output of the powershell hello world script |
| 21 | +- S3 Objects, which uploads the build and Test YAML Files to S3 |
| 22 | +- IAM Policy with permissions to read/write to the S3 Bucket |
| 23 | +- VPC/Subnets/Route Tables/Nat Gateway/etc |
| 24 | + |
| 25 | +## How to Deploy |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | + |
| 29 | +Ensure that you have installed the following tools in your Mac or Windows Laptop before start working with this module and run Terraform Plan and Apply |
| 30 | + |
| 31 | +1. [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) |
| 32 | +2. [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) |
| 33 | + |
| 34 | +### Deployment Steps |
| 35 | + |
| 36 | +#### Step 1: Clone the repo using the command below |
| 37 | + |
| 38 | +```sh |
| 39 | +git clone https://github.com/aws-ia/terraform-aws-ec2-image-builder.git |
| 40 | +``` |
| 41 | + |
| 42 | +#### Step 2: Run Terraform INIT |
| 43 | + |
| 44 | +Initialize a working directory with configuration files |
| 45 | + |
| 46 | +```sh |
| 47 | +cd examples/linux/ |
| 48 | + |
| 49 | +terraform init |
| 50 | +``` |
| 51 | + |
| 52 | +#### Step 3: Run Terraform PLAN |
| 53 | + |
| 54 | +Verify the resources created by this execution |
| 55 | + |
| 56 | +```sh |
| 57 | +export AWS_REGION=<ENTER YOUR REGION> # default set to `us-west-2` |
| 58 | +terraform plan |
| 59 | +``` |
| 60 | + |
| 61 | +#### Step 4: Finally, Terraform APPLY |
| 62 | + |
| 63 | +Create the resources |
| 64 | + |
| 65 | +```sh |
| 66 | +terraform apply |
| 67 | +``` |
| 68 | + |
| 69 | +Enter `yes` to apply. |
| 70 | + |
| 71 | +#### Step 5: Verify the AMI on AWS Console |
| 72 | + |
| 73 | +Output of Terraform apply should look similar |
| 74 | + |
| 75 | +```sh |
| 76 | +module.ec2-image-builder.aws_imagebuilder_image.imagebuilder_image[0]: Creation complete after 1h3m56s [id=arn:aws:imagebuilder:ap-southeast-2:XXXXXXXX:image/myfirstpipeline-image-recipe/0.0.2/1] |
| 77 | +``` |
| 78 | + |
| 79 | +Login to AWS Console, go to the AWS Region where resources are deployed, and go to the location showed on the Terraform output, for example: |
| 80 | + |
| 81 | +EC2 Image Builder > Images > "myfirstpipeline-image-recipe 0.0.2" > "myfirstpipeline-image-recipe 0.0.2/1" |
| 82 | + |
| 83 | +You can see the AMI ID on the output resources: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## Cleanup |
| 88 | + |
| 89 | +To clean up your environment, destroy the Terraform module. |
| 90 | + |
| 91 | +*NOTE:* Empty the S3 bucket created by this module before executing the `terraform destroy` |
| 92 | + |
| 93 | +```sh |
| 94 | +terraform destroy -auto-approve |
| 95 | +``` |
0 commit comments