banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

 AWS-CDK Lambda

This example walks you through creating the resources for a simple hello lambda.

Create project directory

Create an empty directory on your system:

mkdir cdk-workshop && cd cdk-workshop

 Cdk init

We will use cdk init to create a new TypeScript CDK project:

cdk init sample-app --language typescript

We will observe the below output

Applying project template app for typescript
Initializing a new git repository...
Executing npm install...
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN tst@0.1.0 No repository field.
npm WARN tst@0.1.0 No license field.

# Welcome to your CDK TypeScript project!

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`CdkWorkshopStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands


 * `npm run build`   compile typescript to js
 * `npm run watch`   watch for changes and compile
 * `npm run test`    perform the jest unit tests
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk synth`       emits the synthesized CloudFormation template
You can use any IDE, in this we are using VSCODE.

Open the project directory.
  • bin/cdk-workshop.ts is the entrypoint of the CDK application. It will load the stack defined in lib/cdk-workshop-stack.ts
  • lib/cdk-workshop-stack.ts  is where your CDK application’s main stack is defined.
  • package.json is your npm module manifest. It includes information like the name of your app, version, dependencies and build scripts like “watch” and “build”.
  • cdk.json tells the toolkit how to run your app.

App's entry point

Let’s have a quick look at bin/cdk-workshop.ts

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkWorkshopStack } from '../lib/cdk-workshop-stack';

const app = new cdk.App();
new CdkWorkshopStack(app, 'CdkWorkshopStack');

The project created by cdk init sample-app includes an SQS queue, and an SNS topic. We’re not going to use them in our project, so remove them from your the CdkWorkshopStack constructor.

Open and clean it up lib/cdk-workshop-stack.ts. Eventually it should look like this:

import * as cdk from 'aws-cdk-lib';

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

    // No code here!
  }
}

Lambda Handler code

We’ll start with the AWS Lambda handler code.

  • Create a directory lambda in the root of your project tree (next to bin and lib).
  • Add a file called lambda/helloworld.js with the following contents:
    exports.handler = async function(event) {  return {
        statusCode: 200,
        headers: { "Content-Type": "text/plain" },
        body: `Hello world!\n`
      };
    };

This is a simple Lambda function which returns the text “Hello world".

Add AWS lambda function to your stack

Add an import statement at the beginning of lib/cdk-workshop-stack.ts 

import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';























































export class CdkWorkshopStack extends cdk.Stack {


















constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props);



// defines an AWS Lambda resource const hello = new lambda.Function(this, 'HelloHandler', { runtime: lambda.Runtime.NODEJS_14_X, // execution environment code: lambda.Code.fromAsset('lambda'), // code loaded from "lambda" directory handler: 'hello.handler' // file is "hello", function is "handler" });






















}






}


Deploy

Let’s deploy:

cdk deploy

For testing the function test lambda.

No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff