Skip to content

Latest commit

 

History

History
145 lines (98 loc) · 4.5 KB

aws-lambda.md

File metadata and controls

145 lines (98 loc) · 4.5 KB

AWS Lambda

Today I learnt ...

... how to quickly iterate on a nodejs function that uses other AWS services

2024-01-23T12:32:03+00:00 A nodejs script with dependencies can be zipped and function code updated using the aws-cli

Prerequisites

Recipe

  1. Create the Role

    Create the role and attach a policy to the role that grants permissions to the lambda function to log to CloudWatch

    Expand for code
    pwd /home/jon/code/today-i-learned
    cd assets/aws-lambda
    aws iam create-role --role-name til-lambda-example-role --assume-role-policy-document file://trust-policy.json;
    
    aws iam attach-role-policy --role-name til-lambda-example-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    

  2. Create a barebones Lambda with aws-cli

    See the start version of the lambda: assets/aws-lambda/start/index.js

    Add this file to a zip and run aws lambda create-function populating the ARN to the role with your AWS account number.

    Expand for code
    cd assets/aws-lambda/start/
    zip -r9 lambda.zip index.js package.json;
    
    aws lambda create-function --function-name til-lambda-example --runtime nodejs20.x --zip-file fileb://lambda.zip --handler index.handler --role "arn:aws:iam::YOUR_ACCOUNT_NUMBER:role/til-lambda-example-role"
    

  3. Invoke the Lambda with aws-cli

    To test the basic lambda has been created, invoke it passing a sample payload event.json

    After running the code, look in response.json

    Expand for code
    aws lambda invoke --function-name til-lambda-example --payload file://event.json response.json;
    
    cat response.json
    {"headers":{"Content-Type":"application/json"},"statusCode":200,"body":"{\"success\":true}"}%    
    

  4. Now we know we can invoke a simple lambda, let's try one with packages.

    We'll use the existing lambda function and use the https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-code.html aws-cli method to update the code.

    In the script's directory, install packages, create a new zip, and update the function code.

    Expand for code
    cd ../packages
    npm install
    
    zip -r9 lambdapackages.zip .
    aws lambda update-function-code --function-name til-lambda-example --zip-file fileb://lambdapackages.zip
    
    # Invoke lambdapackages remotely
    aws lambda invoke --function-name til-lambda-example --payload file://event.json response.json
    cat response.json
    {"headers":{"Content-Type":"application/json"},"statusCode":200,"body":"{\"success\":true}"}%    

  5. Iterate accordingly.

    Modify your code, rezip, update the function code and run it

    Expand for code
    rm -rf lambdapackages.zip; 
    
    zip -r9 lambdapackages.zip .
    aws lambda update-function-code --function-name til-lambda-example --zip-file fileb://lambdapackages.zip
    
    
    aws lambda invoke --function-name til-lambda-example --payload file://event.json response.json
    cat response.json
    {"headers":{"Content-Type":"application/json"},"statusCode":200,"body":"{\"success\":true}"}%    

  6. Remove role and lambda when you've finished.

    Expand for code
    aws lambda delete-function --function-name til-lambda-example;
    
    aws iam delete-role --role-name til-lambda-example-role

Resources