NashTech Blog

Table of Contents
Using AWS CDK to build an enclave EC2

This blog demo how to use AWS CDK to create an EC2 with an enclave option. After that, we can use an enclave for security. Firstly, clone this repository to start your tutorial.

Read some documents below for information.

Prerequisites before using AWS CDK

  1. Need Node.js 14.15.0 or later.
  2. Install TypeScript
    npm i -g typescript
  3. Enable & set Identity Management Center for AWS SSO.
    Enable IAM Identity Center
  4. Account have permission to deploy
  5. Using the CLI command to configure the login account
    AWS Configure SSO

    After logging in to the web browser, we continue to input information.
    AWS Configure Profile
  6. Replace the key pair with your key pair.
    AWS Key Pair

Code – Build and Run

Using CDK to build infra in AWS, we can use three construct levels:

  • L1 constructs (low-level construct): we call CFN Resources. These constructs directly represent all resources available in AWS CloudFormation.
  • L2 constructs (AWS Resources, but with higher level, intent-based API): AWS constructs offer convenient defaults and reduce the need to know all the details about the AWS resources they represent
  • L3 constructs (patterns): These constructs are designed to help you complete common tasks in AWS, often involving multiple kinds of resources.

We should use L2 constructs for ease. But in L2, we currently don’t have an enclave option, so we will combine L1 and L2.

Using AWS CDK – Steps by Steps

(Optional) Build code to check error

npm run build

Login to AWS Account

aws sso login
Using AWS CDK build enclave EC2 - AWS SSO Login

(Optional) Generate AWS CloudFormation template (That will auto-run when we deploy)

cdk synth
Using AWS CDK - Generate AWS CloudFormation Template

Deploy to AWS

cdk deploy

Check CloudFormation Stack

CloudFormation Stack dashboard after using aws cdk deploy

Go to EC2 Instance board

EC2 instance dashboard

Using SSH to connect to EC2 for testing

Using SSH to connect to EC2 instance
SSH command to connect

Check the log to make sure we finish the building process

sudo su
cd /var/log
tail -n100 cloud-init-output.log

Set configure to run specific region

aws configure

Run command to check enclaves.

nitro-cli describe-enclaves

Check the result from the console.

nitro-cli console --enclave-id i-0a8b0095e465ce702-enc18b1d5e61c374bf

Test with encrypt and decrypt KMS.

After running success, check the EC2 to get information

We can see the role of EC2

We need to create a KMS key with Key Usage: “Encrypt and decrypt” and set a policy to allow the EC2 role to encrypt data. With the Decrypt function, we only allow in Enclave (In debug mode, we need to set “000…0”. But in production mode, we need to set PCR0 value)

SSH to confidential computing EC2. Run command to encrypt data.

KMS_KEY_ARN="alias/kms-for-enclave-testing"
MESSAGE="Hello everyone"
CIPHERTEXT=$(aws kms encrypt --key-id "$KMS_KEY_ARN" --plaintext "$MESSAGE" --query CiphertextBlob --output text)
echo $CIPHERTEXT

Run the command to decrypt data; we will get AccessDeniedException.

aws kms decrypt --ciphertext-blob fileb://<(echo $CIPHERTEXT | base64 -d) --key-id "$KMS_KEY_ARN"

Open vsock and test with enclave

Now, we open another terminal, connecting to Confidential Computing EC2 again. Run the vsock command to connect to the KMS service.

CMK_REGION=us-east-1 # The region where you created your AWS KMS CMK
vsock-proxy 8000 kms.$CMK_REGION.amazonaws.com 443

Using docker to run kmstool-instance, which will allow kmstool-instance to have the same role as the parent instance.

CMK_REGION=us-east-1 # Must match above
ENCLAVE_CID=$(nitro-cli describe-enclaves | jq -r .[0].EnclaveCID)
# Run docker with network host to allow it to fetch IAM credentials with IMDSv2
docker run --network host -it kmstool-instance /kmstool_instance --cid "$ENCLAVE_CID" --region "$CMK_REGION" "$CIPHERTEXT"

(Optional) Destroy all AWS Resources after testing

cdk destroy

Summary

After the demo, you can use the enclave to integrate with KMS and secure your encryption. Besides that, you can know how to use CDK to build your EC2.

Picture of Trần Minh

Trần Minh

I'm a solution architect at NashTech. I live and work with the quote, "Nothing is impossible; Just how to do that!". When facing problems, we can solve them by building them all from scratch or finding existing solutions and making them one. Technically, we don't have right or wrong in the choice. Instead, we choose which solutions or approaches based on input factors. Solving problems and finding reasonable solutions to reach business requirements is my favorite.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top