Skip to content

Commit

Permalink
Add provisioned concurrency in raw Lambda example (pulumi#490)
Browse files Browse the repository at this point in the history
* Add provisioned concurrency in raw Lambda example

* Space

* Lint
  • Loading branch information
mikhailshilkov authored and stack72 committed Dec 4, 2019
1 parent 8d29a4c commit 4eaa5c9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 41 deletions.
107 changes: 67 additions & 40 deletions aws-ts-serverless-raw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,70 @@ The deployed Lambda function is a simple C# application, highlighting the abilit
in a Pulumi application, even if your Pulumi code is written in a different language like JavaScript or Python.

The Lambda function is a C# application using .NET Core 2.1 (a similar approach works for any other language supported by
AWS Lambda). To deploy the complete application:

```bash
# Create and configure a new stack
$ pulumi stack init testing
$ pulumi config set aws:region us-east-2

# Install dependencies
$ npm install

# Build the C# app
$ cd ./app
$ dotnet publish
$ cd ..

# Preview and run the deployment
$ pulumi up
Previewing changes:
...
Performing changes:
...
info: 9 changes performed:
+ 9 resources created
Update duration: 25.017340162s

# Test it out
$ curl $(pulumi stack output endpoint)/hello
{"Path":"/hello","Count":0}

# See the logs
$ pulumi logs -f
2018-03-21T18:24:52.670-07:00[ mylambda-d719650] START RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7 Version: $LATEST
2018-03-21T18:24:56.171-07:00[ mylambda-d719650] Getting count for '/hello'
2018-03-21T18:25:01.327-07:00[ mylambda-d719650] Got count 0 for '/hello'
2018-03-21T18:25:02.267-07:00[ mylambda-d719650] END RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7
2018-03-21T18:25:02.267-07:00[ mylambda-d719650] REPORT RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7 Duration: 9540.93 ms Billed Duration: 9600 ms Memory Size: 128 MB Max Memory Used: 37 MB

# Remove the app
$ pulumi destroy
```
AWS Lambda).

## Deploying and running the Pulumi App

1. Create a new stack:

```bash
$ pulumi stack init dev
```

1. Restore NPM modules via `npm install` or `yarn install`.

1. Build the C# application.

```bash
dotnet publish app
```

1. Set the AWS region:

```bash
$ pulumi config set aws:region us-east-2
```

1. Optionally, set AWS Lambda provisioned concurrency:

```bash
$ pulumi config set provisionedConcurrency 1
```

1. Run `pulumi up` to preview and deploy changes:

```
$ pulumi up
Previewing update (dev):
...
Updating (dev):
...
Resources:
+ 10 created
Duration: 1m 20s
```

1. Check the deployed GraphQL endpoint:

```
$ curl $(pulumi stack output endpoint)/hello
{"Path":"/hello","Count":0}
```

1. See the logs

```
$ pulumi logs -f
2018-03-21T18:24:52.670-07:00[ mylambda-d719650] START RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7 Version: $LATEST
2018-03-21T18:24:56.171-07:00[ mylambda-d719650] Getting count for '/hello'
2018-03-21T18:25:01.327-07:00[ mylambda-d719650] Got count 0 for '/hello'
2018-03-21T18:25:02.267-07:00[ mylambda-d719650] END RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7
2018-03-21T18:25:02.267-07:00[ mylambda-d719650] REPORT RequestId: d1e95652-2d6f-11e8-93f6-2921c8ae65e7 Duration: 9540.93 ms Billed Duration: 9600 ms Memory Size: 128 MB Max Memory Used: 37 MB
```

## Clean up

1. Run `pulumi destroy` to tear down all resources.

1. To delete the stack itself, run `pulumi stack rm`. Note that this command deletes all deployment history from the Pulumi Console.
13 changes: 13 additions & 0 deletions aws-ts-serverless-raw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const policy = new aws.iam.RolePolicy("mylambda-policy", {
}),
});

// Read the config of whether to provision fixed concurrency for Lambda
const config = new pulumi.Config();
const provisionedConcurrentExecutions = config.getNumber("provisionedConcurrency");

// Create a Lambda function, using code from the `./app` folder.
const lambda = new aws.lambda.Function("mylambda", {
runtime: aws.lambda.DotnetCore2d1Runtime,
Expand All @@ -57,13 +61,22 @@ const lambda = new aws.lambda.Function("mylambda", {
timeout: 300,
handler: dotNetApplicationEntryPoint,
role: role.arn,
publish: !!provisionedConcurrentExecutions, // Versioning required for provisioned concurrency
environment: {
variables: {
"COUNTER_TABLE": counterTable.name,
},
},
}, { dependsOn: [policy] });

if (provisionedConcurrentExecutions) {
const concurrency = new aws.lambda.ProvisionedConcurrencyConfig("concurrency", {
functionName: lambda.name,
qualifier: lambda.version,
provisionedConcurrentExecutions,
});
}

///////////////////
// APIGateway RestAPI
///////////////////
Expand Down
2 changes: 1 addition & 1 deletion aws-ts-serverless-raw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "serverless-raw",
"version": "0.1.0",
"dependencies": {
"@pulumi/aws": "^1.0.0",
"@pulumi/aws": "^1.14.0",
"@pulumi/pulumi": "^1.0.0"
}
}

0 comments on commit 4eaa5c9

Please sign in to comment.