Skip to content

Commit 77049a9

Browse files
committed
PowerShell sample app
1 parent ff58bd0 commit 77049a9

File tree

9 files changed

+133
-1
lines changed

9 files changed

+133
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
BUCKET_ID=$(dd if=/dev/random bs=8 count=1 2>/dev/null | od -An -tx1 | tr -d ' \t\n')
3+
BUCKET_NAME=lambda-artifacts-$BUCKET_ID
4+
echo $BUCKET_NAME > bucket-name.txt
5+
aws s3 mb s3://$BUCKET_NAME
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
ARTIFACT_BUCKET=$(cat bucket-name.txt)
4+
cd function
5+
pwsh package.ps1
6+
cd ../
7+
aws cloudformation package --template-file template.yml --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
8+
aws cloudformation deploy --template-file out.yml --stack-name blank-powershell --capabilities CAPABILITY_NAMED_IAM
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
FUNCTION=$(aws cloudformation describe-stack-resource --stack-name blank-powershell --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
4+
5+
while true; do
6+
aws lambda invoke --function-name $FUNCTION --payload '{}' out.json
7+
cat out.json
8+
echo ""
9+
sleep 2
10+
done
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
aws cloudformation delete-stack --stack-name blank-powershell
4+
echo "Deleted function stack"
5+
if [ -f bucket-name.txt ]; then
6+
ARTIFACT_BUCKET=$(cat bucket-name.txt)
7+
while true; do
8+
read -p "Delete deployment artifacts and bucket ($ARTIFACT_BUCKET)?" response
9+
case $response in
10+
[Yy]* ) aws s3 rb --force s3://$ARTIFACT_BUCKET; rm bucket-name.txt; break;;
11+
[Nn]* ) break;;
12+
* ) echo "Response must start with y or n.";;
13+
esac
14+
done
15+
fi
16+
rm -f out.yml out.json
17+
rm -rf function/function.zip
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Blank function (PowerShell)
2+
3+
![Architecture](/sample-apps/blank-powershell/images/sample-blank-powershell.png)
4+
5+
The project source includes function code and supporting resources:
6+
7+
- `function` - A PowerShell function.
8+
- `template.yml` - An AWS CloudFormation template that creates an application.
9+
- `1-create-bucket.sh`, `2-deploy.sh`, etc. - Shell scripts that use the AWS CLI to deploy and manage the application.
10+
11+
Use the following instructions to deploy the sample application.
12+
13+
# Requirements
14+
- [PowerShell Core 6.0 or newer](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell#powershell-core)
15+
- [.NET Core 2.1](https://www.microsoft.com/net/download)
16+
- [AWSLambdaPSCore module](https://www.powershellgallery.com/packages/AWSLambdaPSCore/1.2.0.0)
17+
- The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) to get a Windows-integrated version of Ubuntu and Bash.
18+
- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
19+
20+
# Setup
21+
Download or clone this repository.
22+
23+
$ git clone [email protected]:awsdocs/aws-lambda-developer-guide.git
24+
$ cd aws-lambda-developer-guide/sample-apps/blank-powershell
25+
26+
To create a new bucket for deployment artifacts, run `1-create-bucket.sh`. Or, if you already have a bucket, create a file named `bucket-name.txt` that contains the name of your bucket.
27+
28+
blank-powershell$ ./1-create-bucket.sh
29+
make_bucket: lambda-artifacts-a5e491dbb5b22e0d
30+
31+
# Deploy
32+
To deploy the application, run `2-deploy.sh`.
33+
34+
blank-powershell$ ./2-deploy.sh
35+
Restoring .NET Lambda deployment tool
36+
Initiate packaging
37+
Uploading to e678bc216e6a0d510d661ca9ae2fd941 28800329 / 28800329.0 (100.00%)
38+
Successfully packaged artifacts and wrote output template to file out.yml.
39+
Waiting for changeset to be created..
40+
Waiting for stack create/update to complete
41+
Successfully created/updated stack - blank-powershell
42+
43+
This script uses AWS CloudFormation to deploy the Lambda functions and an IAM role. If the AWS CloudFormation stack that contains the resources already exists, the script updates it with any changes to the template or function code.
44+
45+
# Test
46+
To invoke the function, run `3-invoke.sh`.
47+
48+
blank-powershell$ ./3-invoke.sh
49+
{
50+
"StatusCode": 200,
51+
"ExecutedVersion": "$LATEST"
52+
}
53+
54+
The application uses AWS X-Ray to trace requests. Open the [X-Ray console](https://console.aws.amazon.com/xray/home#/service-map) to view the service map. The following service map shows the function calling Amazon S3.
55+
56+
![Service Map](/sample-apps/blank-powershell/images/blank-powershell-servicemap.png)
57+
58+
Choose a node in the main function graph. Then choose **View traces** to see a list of traces. Choose any trace to view a timeline that breaks down the work done by the function.
59+
60+
![Trace](/sample-apps/blank-powershell/images/blank-powershell-trace.png)
61+
62+
Finally, view the application in the Lambda console.
63+
64+
*To view the output*
65+
1. Open the [applications page](https://console.aws.amazon.com/lambda/home#/applications) in the Lambda console.
66+
2. Choose **blank-powershell**.
67+
68+
![Application](/sample-apps/blank-powershell/images/blank-powershell-application.png)
69+
70+
# Cleanup
71+
To delete the application, run `4-cleanup.sh`.
72+
73+
blank-powershell$ ./4-cleanup.sh

sample-apps/blank-powershell/deploy-powershell.ps1

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New-AWSPowerShellLambdaPackage -ScriptPath ./Handler.ps1 -OutputPackage function.zip
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: 'AWS::Serverless-2016-10-31'
3+
Description: An AWS Lambda application that calls the Lambda API.
4+
Resources:
5+
function:
6+
Type: AWS::Serverless::Function
7+
Properties:
8+
Handler: Handler::Handler.Bootstrap::ExecuteFunction
9+
Runtime: dotnetcore2.1
10+
CodeUri: function/function.zip
11+
Description: Call the AWS Lambda API
12+
Timeout: 30
13+
MemorySize: 512
14+
# Function's execution role
15+
Policies:
16+
- AWSLambdaBasicExecutionRole
17+
- AWSLambdaReadOnlyAccess
18+
- AWSXrayWriteOnlyAccess
19+
Tracing: Active

0 commit comments

Comments
 (0)