Skip to content

harbchun/senpaislist-aws-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

senapaislist-aws-lambda

senpaiXlambda

Creating a new AWS function (with runtime dependencies)

Create the deployment package

# Inside the $(lambda-function-name) directory...

###########################
### Implement something ###
###########################

# Install the runtime dependencies into a seperate directory 
pip install --target ./package $(package-name)
# Create the package
cd package
zip -r ../$(deployment-package-name).zip .
cd ..
zip -g $(deployment-package-name).zip $(lambda_file_name).py

Create the Lambda function

# Inside the $(lambda-function-name) directory...
aws lambda create-function --function-name $(lambda-function-name) --zip-file fileb://$(deployment-package-name).zip --handler $(lambda_file_name).$(lambda_handler_name) --runtime python3.8 --role $(arn)

# $(lambda_handler_name) is the function name inside $(lambda_file_name).py
# arn can be found under the role summary in AWS IAM

Updating an existing AWS function (with runtime dependencies)

Repeat the zipping process from when you had to create a new function

# Inside the $(lambda-function-name) directory... 
zip -r ../$(deployment-package-name).zip .
zip -g $(deployment-package-name).zip $(lambda_file_name).py
# Update
aws lambda update-function-code --function-name senpaislist-broadcast-times --zip-file fileb://broadcast-times-deployment-package.zip

Invoke the Lambda function

aws --cli-read-timeout 0 lambda invoke \
  --function-name $(lambda-function-name) \
      --cli-binary-format raw-in-base64-out \
          --payload '{"key1": "value1", "key2": "value2", "key3": "value3"}' output.txt

Environment variables

ADD

This will replace the entire Variables structure. To retain existing environment variables when you add a new one, include all existing values in your request

aws lambda update-function-configuration --function-name $(lambda-function-name) \
    --environment "Variables={KEY1=value1,KEY2=value2,...}"

Get

aws lambda get-function-configuration --function-name $(lambda-function-name)

Timeout

The default timeout is 3 seconds so if you are creating a new function, change the config to maximum of 900 seconds

aws lambda update-function-configuration --function-name $(lambda-function-name) \
    --timeout 900

Premade Commands for functions

senapaislist-broadcast-times:

update

# update packages
cd package
zip -r ../broadcast-times-deployment-package.zip .
cd ..
# update codes
zip -g broadcast-times-deployment-package.zip get_broadcast_times.py utils/*
# update function
aws lambda update-function-code --function-name senpaislist-broadcast-times --zip-file fileb://broadcast-times-deployment-package.zip

invoke

aws --cli-read-timeout 0 lambda invoke \
  --function-name senpaislist-broadcast-times \
      --cli-binary-format raw-in-base64-out \
          --payload '{"key1": "value1", "key2": "value2", "key3": "value3"}' output.txt

Note

  • The package directories and the deployment zip files will be excluded from the repository, so run:
mkdir package
pip install --target ./package -r requirements.txt

# Then follow the above steps for the zip file

References

Deploy Python Lambda functions with .zip file archives

Lambda update-function-configuration

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages