41 lines
1.0 KiB
Markdown
41 lines
1.0 KiB
Markdown
# Basic Lambda Function with AWS CLI
|
|
|
|
## Basic Lambda Function with AWS CLI
|
|
|
|
```bash
|
|
# Create Lambda execution role
|
|
aws iam create-role \
|
|
--role-name lambda-execution-role \
|
|
--assume-role-policy-document '{
|
|
"Version": "2012-10-17",
|
|
"Statement": [{
|
|
"Effect": "Allow",
|
|
"Principal": {"Service": "lambda.amazonaws.com"},
|
|
"Action": "sts:AssumeRole"
|
|
}]
|
|
}'
|
|
|
|
# Attach basic execution policy
|
|
aws iam attach-role-policy \
|
|
--role-name lambda-execution-role \
|
|
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
|
|
|
|
# Create function from ZIP
|
|
zip function.zip index.js
|
|
aws lambda create-function \
|
|
--function-name my-function \
|
|
--runtime nodejs18.x \
|
|
--role arn:aws:iam::ACCOUNT:role/lambda-execution-role \
|
|
--handler index.handler \
|
|
--zip-file fileb://function.zip \
|
|
--timeout 30 \
|
|
--memory-size 256 \
|
|
--environment Variables={ENV=production,DB_HOST=db.example.com}
|
|
|
|
# Invoke function
|
|
aws lambda invoke \
|
|
--function-name my-function \
|
|
--payload '{"name":"John","age":30}' \
|
|
response.json
|
|
```
|