Demo application to deploy a PowerShell Lambda function using a container image. The container image can be up to 10Gb in size which allows you to build functions larger than the 256MB limit for .zip archive functions. This allows you to include the entire AWSTools for PowerShell SDK, for example.
The container image contains the following components:
- PowerShell custom runtime based on
provided.al2023
. This downloads the specified version of PowerShell and adds the custom runtime files from the PowerShell-runtime folder. - The AWSTools for PowerShell with the entire AWS SDK. You can amend the loaded modules within the Dockerfile to only include specific modules.
AWS.Tools.Common
is required - Lambda function code in the function folder.
You can build and deploy the demo using either of the two options:
- A: Docker Desktop and the AWS CLI
- B: AWS Serverless Application Model (AWS SAM)
Use one of the "Build and deploy" options, A,B depending on your tools.
- Clone the repository and change into the example directory
git clone https://github.com/awslabs/aws-lambda-powershell-runtime
cd aws-lambda-powershell-runtime/examples/demo-container-image-all-aws-sdk
-
Create an Amazon Elastic Container Registry (ECR) repository to store the container image.
-
Login to ECR. Replace the
<region>
and<account>
values.
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account>.dkr.ecr.<region>.amazonaws.com
- Create the repository. Rename
--repository-name
fromdemo-container-image-all-aws-sdk
to your prefered name if required.
aws ecr create-repository --repository-name demo-container-image-all-aws-sdk
- Build the Docker image. Rename the image name in the following steps from
demo-container-image-all-aws-sdk:latest
if required. Replace the<region>
and<account>
values.
docker build -t <account>.dkr.ecr.<region>.amazonaws.com/demo-container-image-all-aws-sdk:latest .
- Push the Docker image. Replace the
<region>
and<account>
values.
docker push <account>.dkr.ecr.<region>.amazonaws.com/demo-container-image-all-aws-sdk:latest
-
Create a Lambda function execution IAM Role using the documentation.
-
Create a Lambda function using the AWS CLI. Replace the
<region>
and<account>
values. Enter the correct IAM Role name for--role
. You can amend the--memory-size
and--timeout
if required.
aws lambda create-function --region <region> --function-name demo-container-image-all-aws-sdk --package-type Image --memory-size 1024 --timeout 30 --code ImageUri=<account>.dkr.ecr.<region>.amazonaws.com/demo-container-image-all-aws-sdk:latest --role "arn:aws:iam::<account>:role/lambda-exec-role"
To make further configuration changes, you can use aws lambda update-function-configuration
. For example, to increase the timeout.
aws lambda update-function-configuration --region <region> --function-name demo-container-image-all-aws-sdk --timeout 45
Once the Lambda function is deployed, you can invoke it using the AWS CLI.
- From a command prompt, invoke the function. Amend the
--function-name
and--region
values for your function.
This should return "StatusCode": 200
for a successful invoke.
aws lambda invoke --function-name demo-container-image-all-aws-sdk --region <region> invoke-result
- View the function results which are outputted to
invoke-result
.
cat invoke-result
B) Build and deploy using AWS Serverless Application Model (AWS SAM).
- Clone the repository and change into the example directory
git clone https://github.com/awslabs/aws-lambda-powershell-runtime
cd aws-lambda-powershell-runtime/examples/demo-container-image-all-aws-sdk
- use
sam build
to build the container image.
sam build
Once the build process is complete, you can use AWS SAM to test the function locally.
sam local invoke
This uses a Lambda-like environment to run the function locally and returns the function response, which is the result of Get-AWSRegion
.
Use AWS SAM to deploy the resources to your AWS account.
- Run a guided deployment to set the default parameters for the first deploy.
sam deploy -g
For subsequent deployments you can use sam deploy
.
- Enter a Stack Name such as
demo-container-image-all-aws-sdk
and accept the remaining initial defaults.
AWS SAM deploys the infrastructure and outputs the function name Value
.
Once the Lambda function is deployed, you can invoke it in the cloud using AWS SAM remote invoke
.
- From a command prompt, invoke the function. Amend the
--stack-name
fromdemo-container-image-all-aws-sdk
if required.
sam remote invoke --stack-name demo-container-image-all-aws-sdk
This should return a successful invoke with the result of Get-AWSRegion
.
Once the Lambda function is deployed, you can also invoke it using the AWS CLI.
- From a command prompt, invoke the function. Amend the
--function-name
and--region
values for your function.
This should return "StatusCode": 200
for a successful invoke.
aws lambda invoke --function-name demo-container-image-all-aw-DemoPowerShellFunction-Nwecb1EWXKq6 --region <region> invoke-result
- View the function result of
AWS-GetRegion
which is outputted toinvoke-result
.
cat invoke-result
To delete the AWS resources created, run the following and confirm that you want to delete the resources that were created by this template.
sam delete