Preparation Steps

In this section, we will prepare an EC2 instance running Amazon Linux 2023 as the working environment, connect to it with VS Code Remote - SSH, and use the AWS CLI to run CloudFormation commands throughout the workshop.

Sign in to the AWS Management Console with an IAM user or IAM role that has administrator permissions. Do not use the root user for this workshop.

Create an IAM role

  1. Open IAM.
    • Choose Roles, then choose Create role.
    • For Trusted entity type, choose AWS service.
    • For Use case, choose EC2, then choose Next.

AWS CloudFormation

  1. Search for and select the AdministratorAccess policy. Choose Next.

AWS CloudFormation

  1. For Role name, enter IaC-Role.

AWS CloudFormation

  1. Review the details and choose Create role.

AWS CloudFormation

The AdministratorAccess policy is used only to keep this lab simple. In real environments, grant permissions following the principle of least privilege.

Launch an EC2 instance

  1. Open EC2.
    • Choose Instances, then choose Launch instances.

AWS CloudFormation

  1. For Name, enter cfn-workspace-ec2.
    • Under Application and OS Images, choose Amazon Linux.
    • Choose the AMI Amazon Linux 2023 AMI.
    • Set Architecture to 64-bit (x86).

AWS CloudFormation

  1. For Instance type, choose t3.small.
    • For Key pair (login), choose Create new key pair.
    • Enter Key pair name cfn-workspace-key.
    • Choose Create key pair.
    • The file cfn-workspace-key.pem is downloaded to your computer.

AWS CloudFormation

  1. Under Network settings, keep the default VPC.
    • Choose Create security group.
    • Select Allow SSH traffic from and choose My IP.
    • Choose Launch instance. AWS CloudFormation

If the public IP address of your network changes, update the SSH inbound rule of this security group to your new My IP.

  1. The instance is created successfully
    • Wait until the instance state is Running.
    • Note its Public IPv4 address.

AWS CloudFormation

  1. Choose Actions:

    • Choose Security
    • Choose Modify IAM role

AWS CloudFormation

  1. Select the IAM role IaC-Role and choose Update IAM role.

AWS CloudFormation

Connect VS Code with Remote - SSH

  1. On your computer, move the cfn-workspace-key.pem file into the ~/.ssh/ folder (on Windows, C:\Users\<user>\.ssh\).
    • On macOS or Linux, restrict the file permissions with:

      chmod 400 ~/.ssh/cfn-workspace-key.pem
      

Do not share the .pem file or commit it to a repository. AWS does not keep a copy of the private key; if you lose the file, you must create a new key pair.

  1. Open VS Code.

    • Choose Extensions (or press Ctrl + Shift + X).
    • Search for Remote - SSH.
    • Install the Remote - SSH extension from Microsoft.
  2. Open the Command Palette with Ctrl + Shift + P.

    • Choose Remote-SSH: Open SSH Configuration File....

    • Select your user config file (for example ~/.ssh/config).

    • Add the following configuration, replacing <EC2_Public_IPv4> with the Public IPv4 address

      Host cfn-workspace
        HostName <EC2_Public_IPv4>
        User ec2-user
        IdentityFile ~/.ssh/cfn-workspace-key.pem
      

AWS CloudFormation AWS CloudFormation AWS CloudFormation

The Public IPv4 address of the EC2 instance changes every time the instance is stopped and started again. When that happens, update the HostName value.

  1. Open the Command Palette.
    • Choose Remote-SSH: Connect to Host....
    • Choose cfn-workspace.
    • Choose Linux.
    • Choose Continue.
    • After a few seconds, VS Code connects to the EC2 instance.

AWS CloudFormation 16. Choose Terminal → New Terminal to open the VS Code terminal. - Verify that the AWS CLI is preinstalled on Amazon Linux 2023 and is using the IAM role IaC-Role:

  aws --version
  aws sts get-caller-identity

AWS CloudFormation

- The `Arn` value in the output must contain `IaC-Role`.
- Set the default region of the AWS CLI to the region of the EC2 instance:

  TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
  AWS_REGION=$(curl -sS -H "X-aws-ec2-metadata-token: ${TOKEN}" http://169.254.169.254/latest/meta-data/placement/region)
  aws configure set default.region "${AWS_REGION}"
  aws configure get region

AWS CloudFormation

  1. Install git with dnf:

    sudo dnf install -y git
    git --version
    

AWS CloudFormation

  1. Clone the repository that contains the workshop CloudFormation templates.
    • The day-1, day-2, day-3 folders hold the templates for sections 3, 4, and 5.

      cd ~
      git clone https://github.com/Hieu192/CloudFormation-series.git
      cd CloudFormation-series
      ls
      

AWS CloudFormation