Create an AWS CDK Stack From the Command Line

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

You can create an AWS CDK project entirely on the command. In this lesson, you will learn how to install the AWS CLI to view all of you existing S3 bucks and to initialize a CDK app with TypeScript.

You can find the CLI documentation here at Configuring the AWS CLI

Tomasz Łakomy: [0:00] Before we start, let's make sure that we have the newest version of CDK installed. In order to do that, I'm going to run npm install aws-cdk. To make sure that CDK was successfully installed, I'm going to run cdk --version. Nice.

[0:15] Another thing that we need to do is to make sure that our AWS command line interface has been configured properly. If you don't know how to do this, I recommend checking out this page from AWS documentation. A link to that is in the description to this video. For instance, I can run aws s3 ls, in order to list all S3 buckets in my account, including half-life-3-source-code.

[0:37] Now, with everything prepared, I can go ahead, clear all of that, and create a folder for our project. This project is going to be a bookstore app. I'm going to call it book-store-graphql-api. If I go to this directory, now I can run cdk init app --language, and we're going to choose typescript for this project.

[0:58] This is going to take a second and boot start our entire CDK project for us. Nice. All done. Let me go ahead and open up my Visual Studio Code editor, and we can see all the files that CDK has generated for us. For instance, if you go to lib directory, here we can see our empty stack.

michaelbruns1
michaelbruns1
~ a year ago

This course is written using version 1 of the cdk, while at the time of writting this comment, we are on version 2. If you get stuck, you can refer to this modified code: import * as cdk from 'aws-cdk-lib'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import { Construct } from 'constructs'; import * as appsync from '@aws-cdk/aws-appsync-alpha';

export class BookStoreGraphqlApiStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);

// The code that defines your stack goes here

// Create the AppSync GraphQL API
const api = new appsync.GraphqlApi(this, "MyApi", {
  name: "my-book-api",
  schema: appsync.SchemaFile.fromAsset("graphql/schema.graphql"),
  authorizationConfig: {
    defaultAuthorization: {
      authorizationType: appsync.AuthorizationType.API_KEY,
      apiKeyConfig: {
        name: "MyApiKey",
        expires: cdk.Expiration.after(cdk.Duration.days(365)),
      },
    },
  },
});

const listBooksLamdba = new lambda.Function(this, "ListBooksHandler", {
  code: lambda.Code.fromAsset("functions"),
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: "listBooks.handler",
});

const listBooksDataSource = api.addLambdaDataSource(
  "listBooksDataSource",
  listBooksLamdba,
);

listBooksDataSource.createResolver("ListBooksResolver", {
  typeName: "Query",
  fieldName: "listBooks",
});

} }

Markdown supported.
Become a member to join the discussionEnroll Today