Creating an AWS Lambda function from the AWS cli

AWS CLI script that creates an AWS Lambda function:

AWS, AWS CLI By Feb 11, 2023 No Comments
Here is an AWS CLI script that creates an AWS Lambda function:

First we need to create IAM Role to attach Lambda Function


AWS CLI script to create AWS IAM Role
aws iam create-role --role-name Lambda_Role --assume-role-policy-document '{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' --query "Role.Arn"

Output

"arn:aws:iam::xxxxxxxxxxxx:role/Lambda_Role"

Lambda Function AWS CLI Script

aws lambda create-function \
  --function-name Lambda_function \
  --runtime nodejs12.x \
  --role arn:aws:iam::xxxxxxxxxxx:role/Lambda_Role \
  --handler "index.handler" \
  --zip-file fileb://function.zip \
  --region us-east-1

Output

{
    "FunctionName": "Lambda_function",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxx:function:Lambda_function",
    "Runtime": "nodejs12.x",
    "Role": "arn:aws:iam::xxxxxxxxxxx:role/Lambda_Role",
    "Handler": "index.handler",
    "CodeSize": 20272963,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2023-02-11T14:51:04.540+0000",
    "CodeSha256": "w+RpB/YhWdBZIzquIRNkem48Z0sJ1KG47unN69C2nIU=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "85bcd9fd-3aa8-4038-8da4-5789ff4ea0f6",
    "State": "Pending",
    "StateReason": "The function is being created.",
    "StateReasonCode": "Creating",
    "PackageType": "Zip",
    "Architectures": [
        "x86_64"
    ],
    "EphemeralStorage": {
        "Size": 512
    },
    "SnapStart": {
        "ApplyOn": "None",
        "OptimizationStatus": "Off"
    }
}
Author

I'm Abhay Singh, an Architect with 9 Years of It experience. AWS Certified Solutions Architect.

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *