Project to generate AMIs using the AWS image builder
The workflow to generate AMIs is done using pull requests. Request using PRs provide history, gating, reviewing and an approval process.
Step by step instructions to create a new image:
The first step is to create the definition template:
- Create an Image Builder definition (cloudformation) template and add it to
the Sage-Bionetworks/aws-infra
repo in the
templates/ImageBuilder
directory. - Create a PR with this change to aws-infra repo
- Review, approve and Merge the PR
- Tag the repo with a version number
Next step is to use the definition template to provision an AMI:
- In this repo, add a new Sceptre config in
config/prod
directory referencing a versioned (or tagged) instance of the cloudformation template. TheImageVersion
is arbitrary however it must follow the semantic version convention (i.e. X.Y.Z). We typically start with1.0.0
and increment as needed. - Create a PR to merge the new config into the
main
branch. - Once the PR is merged, the cloudformation template will be deployed to the AWS org-sagebase-imagecentral account which will in turn trigger a build of the image which will generate an AMI.
- The AMI will be publicly shared to ALL AWS accounts which means that it is searchable from any AWS accounts using either the AWS console EC2 Instances->AMIs or Image Builder->Images pages. The AMI is also searchable using the AWS CLI describe-images command.
To update an image we must first update aws-infra then this repo.
Update the cloudformation template in aws-infra:
- Create a PR to update the ImageBuilder cloudformation template in the aws-infra repo
- Review, approve and Merge the PR
- Tag the repo with a version number
Update the Sceptre config in this repo:
- Create a PR to update the template
url
reference - Increment the
ImageVersion
number in the Sceptre config file otherwise cloudformation may fail with a message similar toThe following resource XXXXX already exists..
. - Review, approve and Merge the PR
- Once merged the cloudformation template will be deployed which will trigger an update to the AMI. AMIs are immutable therefore AWS will create a new AMI on every change. The updated AMIs will retain the same name, only the version number is updated.
Important info when removing image builder resources and the generated images.
- Deleting a file from this repo will not automatically remove the cloudformation stack or remove existing image builder resources.
- To delete image builder resources you need to go into the AWS console -> cloudformation then delete the cloudformation stack for it. Note that deleting the cloudformation stack does not delete the generated AMIs.
- Typically the generated AMIs are immutable and should never be deleted however there are situations where it is appropriate to delete AMIs, for example when testing AMIs. To delete AMIs go into the AWS console EC2 -> Images -> AMIs, search for the AMIs then delete (or de-register) them.
NOTE: Step #2 above can also be done using the sceptre delete
command or the AWS CLI.
To test an image we recommend that you manually provision an EC2 instance from the AMI image then connect to it using the AWS session manager or SSH. Validate that the image meets the specifications defined in the cloudformation template and that image is generally running as expected.
Contributions are welcome.
Requirements:
- Install pre-commit app
- Clone this repo
- Run
pre-commit install
to install the git hook.
As a pre-deployment step we syntatically validate our sceptre and cloudformation yaml files with pre-commit.
Please install pre-commit, once installed the file validations will
automatically run on every commit. Alternatively you can manually
execute the validations by running pre-commit run --all-files
.
The value of a stack's stack_name
parameter must match the config's file
name. Stack names can contain only alphanumeric characters (case-sensitive)
and hyphens. They must start with an alphabetic character and can't be longer
than 128 characters.
We use sceptre and cloudformation to deploy resources onto an AWS account.
We have configured github actions to deploy CF template which in turn will build AMI images using the AWS image builder