Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Setting Up and Tearing Down Environments with the AWS CDK

In this lab, we'll use the Cloud9 service in AWS to work on our CDK project using a "blue/green" deployment. Our CDK project already consists of an API Gateway, backed by a Lambda function that can scan items on a DynamoDB table. We want to update our application and add a second Lambda function that can add items to our DynamoDB table. To do this, we'll need to create a second "green" stack with an entirely separate API Gateway, Lambda function, and DynamoDB table. Once the "green" stack has been provisioned and tested, we can promote it to the "blue" stack and then destroy our unneeded infrastructure.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Sep 26, 2022

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create A Cloud9 Development Environment and Resize the Instance Volume to 30 GiB

  2. Challenge

    From the Cloud9 Terminal, Pull the Code from the Provided GitHub Link and Deploy the "Blue" Stack

    • git clone https://github.com/ACloudGuru-Resources/cdk-lab-1.git
    • cd cdk-lab-1
    • npm install
  3. Challenge

    Create a New “Green” Stack and Copy the Constructs from Your "Blue" Stack

  4. Challenge

    In Your “Green” Stack, Create a New API Gateway Endpoint That Calls a New Lambda Function to Add Items to Your DynamoDB Table

    import * as AWSXRay from 'aws-xray-sdk';
    import * as AWSSDK from 'aws-sdk';
    import { APIGatewayProxyEvent } from "aws-lambda";
    
    //define DocumentClient
    const AWS = AWSXRay.captureAWS(AWSSDK);
    const docClient = new AWS.DynamoDB.DocumentClient();
    
    //define table by variable passed from stack
    const table = process.env.DYNAMODB || "undefined"
    
    //putItems function uses params to scan a dynamodb table
    async function putItem(){
      try {
        const data = await docClient.put(params).promise()
        return data
      } catch (err) {
        return err
      }
    }
    
    
    //actual handler logs events and calls scanItems
    //logs error on catch
    exports.handler = async (event:APIGatewayProxyEvent) => {
      try {
          console.log(event)
          console.log(event.body)
          const obj = JSON.parse(event.body)
            
          const ID = obj.id;
          const NAME = obj.name
            
          //define table and item in params
          const params = {
              TableName: table,
              Item: {
                   id: {S: ID},
                   name: {S: NAME}
              }
          };
        const data = await putItem(params)
        return { body: JSON.stringify(data) }
      } catch (err) {
        return { error: err }
      }
    }
    
  5. Challenge

    Deploy Your "Green" Stack and Verify Deployment

  6. Challenge

    Once Your "Green" Stack Is Deployed, Tear Down Your “Blue” Stack

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans