This exercise will show you the steps for exporting and importing Lambda functions between regions manually. If you want more information click here.
Required knowledge:
By the end of this exercise, you will be able to:
Estimated Duration: 30 minutes
Approximate Cost: 1 USD
Go to the AWS Lambda console: https://console.aws.amazon.com/lambda.
For this example, choose the region us-east-1 (Northern Virginia).
In the navigation pane, on the left side of the console, select Functions (Functions).
Choose Create Function (Create Role).
exports.handler = async (event) => {
console.log("chamando função lambda...")
const response = {
statusCode: 200,
body: JSON.stringify('Testando o import e export de funções Lambda!'),
};
return response;
};
Your Lambda function was created successfully! We will now export it from the current region for further import into another region.
If you already have a Lambda function created, you can skip to the next step at Export the Lambda function
Go to the AWS CloudShell console: https://console.aws.amazon.com/cloudshell/home.
For this example, choose the region us-east-1 (Northern Virginia). CloudShell is not yet available in all regions.
With the command prompt ready, we need create the role to allow execution access to the Lambda function:
aws iam create-role --role-name lambda-execution --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
Permission is now required before the function can be executed. To do this, add the policy AWS Lambda Basic Executive Role to the role created above:
aws iam attach-role-policy --role-name lambda-execution --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
We will now create the lambda function via CLI. To do this, run the command below:
echo "exports.handler = async (event) => {
console.log('chamando função lambda...', event);
const response = {
'statusCode': 200,
'body': JSON.stringify('Testando o import e export de funções Lambda!'),
};
return response;
};" > index.js
The above command created the file index.js containing the source code for our Lambda function. Let’s now package the function into a.zip file so we can register it to the AWS Lambda service for the region in question:
zip multi-region-test-function index.js
We can now register the function through the command below:
export ACC_ID=$(aws sts get-caller-identity --query Account --output text)
aws lambda create-function --function-name multi-region-test-function --zip-file fileb://multi-region-test-function.zip --handler index.handler --runtime nodejs14.x --role arn:aws:iam::$ACC_ID:role/lambda-execution
exports.handler = async (event) => {
console.log("chamando função lambda...")
const response = {
statusCode: 200,
body: JSON.stringify('Testando o import e export de funções Lambda!'),
};
return response;
};
(Optional) If the function already exists in the AWS Lambda service, you can update the function source code using the command below:
aws lambda update-function-code --function-name multi-region-test-function --zip-file fileb://multi-region-test-function.zip
Let’s now test the created function. Use the command below to invoke the Lambda function via the command line:
aws lambda invoke --function-name multi-region-test-function --payload $(echo '{ "key_sample": "value_sample" }' | base64) --log-type Tail --query 'LogResult' --output text out | base64 -d
We have an INFO line containing the console message inside the function along with the payload sent, which indicates that the function was executed successfully.
Ready! Your Lambda function was successfully created via the AWS CLI with CloudShell.
aws lambda list-functions --region sa-east-1
aws lambda get-function --function-name multi-region-test-function --query 'Code.Location' | xargs wget -O multi-region-test-function-exp.zip
You can extract other metadata from the Lambda function, such as timeout settings or environment variables, for example. For this lab, we focused only on the source code for the simplicity of the lab
We will update an existing role in another region. In this example, we will use the region sa-east-1 (North America - Sao Paulo). If the role does not already exist in this region, perform the steps in the step Create a Lambda function described at the beginning of this lab.
aws lambda update-function-code --function-name multi-region-test-function --region sa-east-1 --zip-file fileb://multi-region-test-function-exp.zip
aws lambda invoke --function-name multi-region-test-function --payload $(echo '{ "key_sample": "value_sample" }' | base64) --log-type Tail --query 'LogResult' --region sa-east-1 --output text out | base64 -d
We can validate that the function was updated because the INFO log line now also writes the received payload, which did not happen with the function created at the beginning of this lab.
aws lambda delete-function --function-name multi-region-test-function --region us-east-1
aws lambda delete-function --function-name multi-region-test-function --region sa-east-1
With this exercise, it was possible to verify how replication of Lambda functions between AWS regions can be used in DR strategies of type. Backup & Restore and Pilot Light. For strategies for Warm Standby and Active-Active, refer to the practical exercise for deploying lambda functions via CI/CD Pipeline.
Source: AWS Lambda - Developer Guide