Monday, January 16, 2023

AWS EC2 AMI

 

Create Key Pair

  • Run the CLI command below, and you will see a private key is generated
aws ec2 create-key-pair --key-name $KEY_NAME \
    --query KeyMaterial \
    --output text > $KEY_NAME.pem


Launch a EC2

  • Set up BaseName, KeyPairName for a EC2 instance
  • Run the CloudFormation template to deploy a EC2 instance for AMI

aws cloudformation create-stack \
    --stack-name $STACK_NAME \
    --template-body file://cfn/ami/cfn-ec2.yml \
    --cli-input-json file://cfn/ami/cfn-ec2.parameters-cli.json


Connect through Systems Manager RDP

  • Systems Manager > Fleet Manager
  • Check the instance to use
  • Node actions > Connect with Remote Desktop
    • Option1: Key pair
      • Choose Key pair
      • Administrator account name: Administrator
      • Browse your local machine to select the key pair file
        • Select $KEY_NAME.pem
    • Option2: Windows password
      • Choose User credential
        • Username: Administrator
        • Password: Retrieve from the CLI command below
INSTANCE_ID=$(aws ec2 describe-instances \
    --filters "Name=tag-value,Values=$BASE_NAME" \
    --query "Reservations[*].Instances[*].[InstanceId]" \
    --output text)

aws ec2 get-password-data \
    --instance-id $INSTANCE_ID \
    --priv-launch-key $KEY_NAME.pem


Install software

  • Install necessary software
    • Git
    • Powershell
    • GitLab-Runner
    • AWS CLI

Sysprep settings

  • Windows program search > Amazon EC2Launch settings
    • General > Set administrator account
      • Choose Random (retrieve from console)
    • Prepare for imaging
      • Shutdown with Sysprep
    • You have unsaved changes. Do you want to prepare the instance to create an image, execute Sysprep, and then shut it down?
      • Yes

Generate AMI

  • Run the CLI command below to create an AMI

aws ec2 create-image \
    --instance-id $INSTANCE_ID \
    --name $AMI_NAME \
    --description "An AMI for CloudFormation"


No comments:

Post a Comment