Lambda with CDK Overview

Overview

cdk lambda

Directory structure

infra

This contains the cdk made infrastructure.

app

This contains the lambda app

dist

This containts the compiled lambda app.

Quick Walkthtough

Prerequisites: CDK and Task installed.

  1. Change to lambda-go/lambda-cdk/hello-world of the source

    cd lambda-go/lambda-cdk/hello-world
    
  2. Show all available tasks:

    task -l
    

    Output:

    task: Available tasks for this project:
    * app:build: 		build go
    * app:fastdeploy: 	Deploy only lambda
    * app:test-app: 	all go test
    * deploy: 		deploy stack without asking
    * destroy: 		destroy stack without asking
    * diff: 		Show stack differences
    * list: 		List stacks
    * update: 		install latest cdk and other modules
    
  3. Show stack. This also shows you whether go an CDK are installed:

    task list
    

    Output:

    hello
    
  4. Build lambda function and deploy stack with lambda resource

    task deploy
    

    Output:

    hello: deploying...
    ...
    hello: creating CloudFormation changeset...
    ...
    10:48:37 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | hello
    ...
    ✅  hello
    
    Stack ARN:
    arn:aws:cloudformation:eu-central-1:555544443333:stack/hello/a9d0d2e0-3702-11ec-8056-0a6a01a6fd8c   
    

    Now the lambda resource and the function is deployed and you can execute it with the following event file:

    {
    "name": "hugo"
    }
    

    Output:

    Loading response...
    Invocation result for arn:aws:lambda:eu-central-1:555544443333:function:sayhello
    Logs:
    START RequestId: 5a82f034-d327-486a-b626-96ba91c80878 Version: $LATEST
    END RequestId: 5a82f034-d327-486a-b626-96ba91c80878
    REPORT RequestId: 5a82f034-d327-486a-b626-96ba91c80878	Duration: 0.58 ms	Billed Duration: 1 ms	Memory Size: 1024 MB	Max Memory Used: 19 MB	
    
    
    Payload:
    "Hi hugo!"
    
  5. Change lambda code

    • Edit go code
    vi app/main.go
    

    Example: Change line return fmt.Sprintf("Hello %s!", name.Name ), nil to return fmt.Sprintf("HiHo %s!", name.Name ), nil

  6. Deploy only lambda function, the app:

    task app:fastdeploy
    

    Output:

    updating: main (deflated 60%)
    task: [app:fastdeploy] aws lambda update-function-code --function-name  sayhello --zip-file fileb://../dist/main.zip
    {
        "FunctionName": "sayhello",
        "FunctionArn": "arn:aws:lambda:eu-central-1:555544443333:function:sayhello",
        "Runtime": "go1.x",
        "Role": "arn:aws:iam::555544443333:role/hello-simplelambdaServiceRole98D3F52B-ZJUEUX694L3Q",
        "Handler": "main",
        "CodeSize": 2380092,
        "Description": "Simple Lambda says hello",
        "Timeout": 10,
        "MemorySize": 1024,
        "LastModified": "2021-10-27T08:57:45.091+0000",
        "CodeSha256": "6q8wMMXDYskOqGk176kHyCEbxUrPClrhPG7JBjbMMbk=",
        "Version": "$LATEST",
        "TracingConfig": {
            "Mode": "PassThrough"
        },
        "RevisionId": "abcb74c9-3c72-400e-a979-e3c54a6a2559",
        "State": "Active",
        "LastUpdateStatus": "Successful",
        "PackageType": "Zip",
        "Architectures": [
            "x86_64"
        ]
    }
    
  7. Invoke changed Lambda

    Output :

        Payload:
            "Hiho hugo!"
    
  8. Cleanup: Delete

        task destroy
    

After the quick walkthrough I will explain the components in the next chapters, First, the construct.

See also

Source

See the full source on github.

Sources