Package: cdk-create-ami
cdk-create-amiAn AWS Cloud Development Kit (AWS CDK) construct library that allows you to build anAmazon EC2instance and create anAmazon Machine Imagefrom that instance.BackgroundFromCreating an AMI:You can launch an instance from an existing AMI, customize the instance (for example,install softwareon the instance), and then save this updated configuration as a custom AMI. Instances launched from this new custom AMI include the customizations that you made when you created the AMI.This CDK Construct will assist in you creating that AMI for later use.InstallingTo add to your AWS CDK package.json file:yarn add cdk-create-amiHow It WorksTo create an image:import*asamifrom'cdk-create-ami';constamiImage=newami.CreateAMI(this,'amiImage',{instanceId:baseInstance.instanceId,deleteAmi:true,deleteInstance:true,blockDeviceMappings:[{deviceName:'/dev/sdh',ebs:{volumeSize:20,volumeType:ami.VolumeType.GP3,deleteOnTermination:true,},},],tagSpecifications:[{resourceType:ami.ResourceType.IMAGE,tags:[{key:'TagKey',value:'TagValue'}],},],});This will take an already createdInstanceand convert it to an AMI. This process involves stopping the Instance and then creating the AMI. Besides typical AMI creation options regardingblock device mappingsandtags, the AMI can be created with two options:deleteAmi:true,deleteInstance:true,deleteAmi: When the CDK is destroyed, if this is set toTrue, the AMI will be deleted. Otherwise, the AMI will be retained.deleteInstance: When the CDK is deployed, if this is set toTrue, the base image will be deleted. Otherwise, the instance will be retained in aStoppedstate.ExampleThe associated exmaple includes two Stacks that can be created.AMIExample - Deploys Instance and creates AMI from InstanceInstanceExample - Deploys Instance from previously created AMIAMI Examplecd example
yarn amiThis will deploy an Instance and create an AMI from the created Instance.Creating the Instanceconstvpc=newVPC(this,'VPC');constbaseInstance=newBaseInstance(this,'Instance',{vpc:vpc.vpc,securityGroup:vpc.securityGroup,ec2Role:vpc.ec2Role,});The example will create a VPC and Instance to be used to create the AMI. This instance contains acloud-init script:HOMEDIR=/home/ec2-user
yumupdate-y
yuminstallnet-tools-y
yuminstallwget-yTOKEN=`curl-XPUT"http://169.254.169.254/latest/api/token"-H"X-aws-ec2-metadata-token-ttl-seconds: 21600"`LOCAL_HOSTNAME=$(curl-H"X-aws-ec2-metadata-token:$TOKEN"-vhttp://169.254.169.254/latest/meta-data/public-hostname)INSTANCE_ID=$(curl-H"X-aws-ec2-metadata-token:$TOKEN"-vhttp://169.254.169.254/latest/meta-data/instance-id)echo"AMI Hostname:$LOCAL_HOSTNAME">>/home/ec2-user/config.txtecho"AMI InstanceId:$INSTANCE_ID">>/home/ec2-user/config.txtThis will result in a file:/home/ec2-user/config.txtthat contains the base instance information. This file will be retained in the AMI.AMI Hostname: ec2-54-152-127-245.compute-1.amazonaws.com
AMI InstanceId: i-002aeb6cdde92c9b5SSMThe AMI information will be stored in AWS Systems Manager - Parameter StoreInstance ExampleOnce the AMI has been created, that AMI can be used to create a new Instance that has been pre-configured.yarninstanceimageId=IMAGEIDThe IMAGEID to be used is provided as output from the AMI Example Stack.This AMI Image ID is retrieved by the CDK:constcustomAmi=newec2.GenericSSMParameterImage('/createAMI/'+props.imageId,ec2.OperatingSystemType.LINUX,);This AMI Image ID is used to create the new Instance:constec2Instance=newec2.Instance(this,'Instance',{vpc:props.vpc,instanceType:ec2.InstanceType.of(ec2.InstanceClass.T4G,ec2.InstanceSize.MEDIUM,),machineImage:customAmi,//MoreInstanceconfigurationhere});This instance contains acloud-init script:HOMEDIR=/home/ec2-userTOKEN=`curl-XPUT"http://169.254.169.254/latest/api/token"-H"X-aws-ec2-metadata-token-ttl-seconds: 21600"`LOCAL_HOSTNAME=$(curl-H"X-aws-ec2-metadata-token:$TOKEN"-vhttp://169.254.169.254/latest/meta-data/public-hostname)INSTANCE_ID=$(curl-H"X-aws-ec2-metadata-token:$TOKEN"-vhttp://169.254.169.254/latest/meta-data/instance-id)echo"New Instance Hostname:$LOCAL_HOSTNAME">>/home/ec2-user/config.txtecho"New Instance InstanceId:$INSTANCE_ID">>/home/ec2-user/config.txtBecause the Instance used to create the AMI has already updated and installed packages, these do not need to be run again. Instead, the new Instance Hostname and InstanceId will be copied to the existing/home/ec2-user/config.txtfile.AMI Hostname: ec2-54-152-127-245.compute-1.amazonaws.com
AMI InstanceId: i-002aeb6cdde92c9b5
New Instance Hostname: ec2-54-242-5-47.compute-1.amazonaws.com
New Instance InstanceId: i-0e67014a77d5d1995ContributingSeeCONTRIBUTINGfor more information.LicenseThis project is licensed under the Apache-2.0 License.