Configure VSCode environment

Configure VSCode environment

  1. Open the Visual Studio Code in your PC

    • Select the Extension icon or use Ctrl + Shift + X to open the Extension interface.
    • Find Remote SSH, select Remote - SSH and Remote - SSH: Editing Configuration Files
    • Install these extensions.

install-ssh-extensions-1 install-ssh-extensions-2

  1. After the installation is complete, open Command Palette with Ctrl + Shift + P

    • Find Remote-SSH: Add New SSH Host.
    • Enter cdk-workspace-ec2.
    • Then select C:\…\.ssh\config, then the configuration file will be opened.

add-new-ssh-host enter-ssh-host open-config

The configuration of SSH Host will have the format like below

Host cdk-workspace
  HostName x.x.x.x
  User ec2-user
  IdentityFile D:\path\to\private-key.pem

update-file-config

  1. We’ll connect to the host which we’ve just added

    • Find Remote-SSH: Connect Current Windows to Host
    • Select the host cdk-workspace
    • Select Linux
    • Select Continue

connect-to-host select-host select-os select-continue

After a few seconds, the connection is established successfully. Now you can open EC2’s directories in your VSCode

  1. Now, we’ll open the root directory

open-directory main-directory

  1. We’ll attach IAM Role to EC2 Instance, back to EC2 console, in Actions

    • Select Security
    • Select Modify IAM Role
    • Select the IAM Role named CDK-Role which you created before

add-iam-role-to-ec2 update-iam-role

  1. Next, copy and Paste the command below into the Terminal of VSCode Workspace to install tools to support text processing on the command line.
sudo yum -y install jq gettext bash-completion moreutils

add-some-utils

  1. Install python 3.9 and pip

Firstly, we need to install these binaries to support python3.9

sudo yum install gcc openssl-devel bzip2-devel libffi-devel

install-support-bin

Enter /opt directory and get python3.9 compress file

cd /opt
wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz
sudo tar xzf Python-3.9.16.tgz

Enter Python-3.9.16 directory and extract the downloaded file, and run configure

cd Python-3.9.16
sudo ./configure --enable-optimizations
sudo make altinstall

install-pyton-1

AWS CDK needs versions of python >= 3.8

install-pyton-2 2.31-python-pip-are-installed

  1. Similar to CloudFormation, you can install the cfn-lint tool to help you check CDK templates and other information, including auditing. Check if the resource properties are correct or not configured according to best practices or not.
pip install cfn-lint

And check the successful installation of cfn-lint using the following command:

cfn-lint --version

install-cfn-lint check-cfn-lint-install

  1. Setup environment variables to let aws cli use the current region

Before we can get metadata of EC2, we need to modify the instance metadata options

modify-instance-metadata-1 modify-instance-metadata-2

Make sure we can get the EC2 instance’s metadata

check-instance-metadata

Set up environment variables

export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
export AZS=($(aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text --region $AWS_REGION))

Save the configuration information to bash_profile

echo "export ACCOUNT_ID=${ACCOUNT_ID}" | tee -a ~/.bash_profile

echo "export AWS_REGION=${AWS_REGION}" | tee -a ~/.bash_profile

echo "export AZS=(${AZS[@]})" | tee -a ~/.bash_profile

aws configure set default.region ${AWS_REGION}

setup-env-add-to-bash-profile

  1. The CDK isn’t installed, we have to install it with NPM. Install NodeJS with NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

install-nvm

  1. Install NodeJS and AWS CDK and check if node and npm are available.
nvm install 20
node -v
npm -v
npm install -g aws-cdk

install-nodejs install-cdk

  1. We will use the command to check if the EC2 instance is using the IAM Role correctly.
aws sts get-caller-identity --query Arn | grep CDK-Role -q && echo "IAM role valid" || echo "IAM role NOT valid"

check-if-role-is-valid