. Advertisement .
..3..
. Advertisement .
..4..
Today, we will learn and discuss ways to create a lambda function with AWS CLI. If you’re looking for the method to that but haven’t gotten what you need, let’s start with this article right away.
What should we do to create a lambda function with AWS CLI?
Deployment package and an execution role are prerequisites for building a lambda function on AWS.
Steps to deploy an AWS Lambda function:
1. Make an IAM user
The IAM user you use with AWS SAM must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
Create a set of access keys after the user has been created.
Save the file locally.
Upload these keys to Okteto as secrets before the installer may utilize them by using AWS ACCESS KEY ID for the secret that holds the access key and AWS SECRET ACCESS KEY for the secret that holds the secret access key.
2. Build a customized installation image
To perform this step, you can use the SAM CLI. Initially, create a Docker image by utilizing the following Dockerfile.
FROM okteto/pipeline-runner:1.0.0
RUN apt-get update && \
apt-get install unzip && \
curl -o /tmp/awscli-exe-linux-x86_64.zip -L https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip && \
unzip /tmp/awscli-exe-linux-x86_64.zip -d /tmp/aws-cli && \
sh /tmp/aws-cli/aws/install && \
aws --version && \
curl -o /tmp/aws-sam-cli-linux-x86_64.zip -L https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip && \
unzip /tmp/aws-sam-cli-linux-x86_64.zip -d /tmp/sam-installation && \
sh /tmp/sam-installation/install && \
sam –version
Everything in the okteto/pipeline-installer image, the AWS CLI, and the SAM CLI will be included in the image produced by building this Dockerfile.
3. Make your Okteto manifest.
It would help if you built an Okteto manifest like the one below to deploy a function in Okteto:
# okteto.yaml
deploy:
- sam build
- sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --s3-prefix "${OKTETO_NAMESPACE}" --stack-name "${OKTETO_NAMESPACE}-okteto-lambda" --resolve-s3
- aws cloudformation describe-stacks --region us-east-1 --stack-name ${OKTETO_NAMESPACE}-okteto-lambda --query "Stacks[0].Outputs[0].OutputValue" --output text > lambda-url
- cat lambda-url
destroy:
- sam delete --no-prompts --stack-name "${OKTETO_NAMESPACE}-okteto-lambda" --region us-east-1
4. Start your development environment
Put the Okteto manifest you made in the previous step into your repository and push the updates to a remote branch.
Use the Okteto dashboard, deploy your development environment. Your development environment will be completely deployed, and your AWS Lambda function will be operational shortly.
Create the execution role
By using the create-role command in the AWS CLI you can create an execution role. In the below example, you determine the trust policy inline. Depending on your shell, there may or may not be a requirement for escaping quotes in the JSON string.
aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
Now you can create functions
aws lambda create-function \
--function-name my-lambda-function-name \
--runtime python3.6 \
--role arn:aws:iam::123456789012:role/service-role/MyTestFunction-role-tges6bf \
--handler my_pythonfile_without_py.my_handler_func \
--zip-file fileb://my-function.zip
Conclusion
ITtutoria hopes you’ve been able to create a lambda function in AWS. Don’t forget to leave your thoughts and opinions below in the comment section. Thank you for taking the time to read the article. Best of luck to you!
Read more:
Leave a comment