Skip to content

Commit

Permalink
1. README.md update netcore version 2.aws-py-serverless-raw fix provi…
Browse files Browse the repository at this point in the history
…sioned_concurrent_executions 3.aws-py-serverless-raw fix Pulumi.yaml
  • Loading branch information
XUANHE ZHOU committed Jun 30, 2020
1 parent 723d5b5 commit 07d80a0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 72 deletions.
13 changes: 9 additions & 4 deletions aws-py-serverless-raw/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
name: aws-py-serverless-raw
runtime:
name: python
description: A minimal Python Pulumi program
name: serverless-raw
runtime: python
description: Basic example of a serverless AWS application
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-east-2

12 changes: 8 additions & 4 deletions aws-py-serverless-raw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ to accomplish all of the same things this higher-level package offers.
The deployed Lambda function is a simple C# application, highlighting the ability to manage existing application code
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
The Lambda function is a C# application using .NET Core 3.1 (a similar approach works for any other language supported by
AWS Lambda).

## Deploying and running the Pulumi App

1. Install dependencies. In this example we will install them in a virtual environment named `venv`.
```bash
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
```

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
Expand Down
126 changes: 63 additions & 63 deletions aws-py-serverless-raw/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

# A DynamoDB table with a single primary key
counter_table = aws.dynamodb.Table("counterTable",
attributes=[
{
"name": "Id",
"type": "S",
},
],
hash_key="Id",
read_capacity=1,
write_capacity=1,
)
attributes=[
{
"name": "Id",
"type": "S",
},
],
hash_key="Id",
read_capacity=1,
write_capacity=1,
)

##################
## Lambda Function
Expand All @@ -43,25 +43,25 @@
}])

role = aws.iam.Role("mylambda-role",
assume_role_policy=instance_assume_role_policy.json,
)
assume_role_policy=instance_assume_role_policy.json,
)

policy = aws.iam.RolePolicy("mylambda-policy",
role=role,
policy=Output.from_input({
"Version": "2012-10-17",
"Statement": [{
"Action": ["dynamodb:UpdateItem", "dynamodb:PutItem", "dynamodb:GetItem",
"dynamodb:DescribeTable"],
"Resource": counter_table.arn,
"Effect": "Allow",
}, {
"Action": ["logs:*", "cloudwatch:*"],
"Resource": "*",
"Effect": "Allow",
}],
}),
)
role=role,
policy=Output.from_input({
"Version": "2012-10-17",
"Statement": [{
"Action": ["dynamodb:UpdateItem", "dynamodb:PutItem", "dynamodb:GetItem",
"dynamodb:DescribeTable"],
"Resource": counter_table.arn,
"Effect": "Allow",
}, {
"Action": ["logs:*", "cloudwatch:*"],
"Resource": "*",
"Effect": "Allow",
}],
}),
)

# Read the config of whether to provision fixed concurrency for Lambda
config = pulumi.Config()
Expand All @@ -70,29 +70,29 @@
# Create a Lambda function, using code from the `./app` folder.

lambda_func = aws.lambda_.Function("mylambda",
opts=pulumi.ResourceOptions(depends_on=[policy]),
runtime="dotnetcore3.1",
code=pulumi.AssetArchive({
".": pulumi.FileArchive(dotnet_application_publish_folder),
}),
timeout=300,
handler=dotnet_application_entry_point,
role=role.arn,
publish=bool(provisioned_concurrent_executions),
# Versioning required for provisioned concurrency
environment={
"variables": {
"COUNTER_TABLE": counter_table.name,
},
},
)
opts=pulumi.ResourceOptions(depends_on=[policy]),
runtime="dotnetcore3.1",
code=pulumi.AssetArchive({
".": pulumi.FileArchive(dotnet_application_publish_folder),
}),
timeout=300,
handler=dotnet_application_entry_point,
role=role.arn,
publish=bool(provisioned_concurrent_executions),
# Versioning required for provisioned concurrency
environment={
"variables": {
"COUNTER_TABLE": counter_table.name,
},
},
)

if provisioned_concurrent_executions:
concurrency = aws.lambda_.ProvisionedConcurrencyConfig("concurrency",
function_name=lambda_func.name,
qualifier=lambda_func.version,
provisioned_concurrent_executions=1
)
function_name=lambda_func.name,
qualifier=lambda_func.version,
provisioned_concurrent_executions=provisioned_concurrent_executions,
)


#####################
Expand Down Expand Up @@ -130,31 +130,31 @@ def swagger_route_handler(lambda_arn):

# Create the API Gateway Rest API, using a swagger spec.
rest_api = aws.apigateway.RestApi("api",
body=lambda_func.arn.apply(lambda lambda_arn: swagger_spec(lambda_arn)),
)
body=lambda_func.arn.apply(lambda lambda_arn: swagger_spec(lambda_arn)),
)

# Create a deployment of the Rest API.
deployment = aws.apigateway.Deployment("api-deployment",
rest_api=rest_api,
# Note: Set to empty to avoid creating an implicit stage, we'll create it
# explicitly below instead.
stage_name="")
rest_api=rest_api,
# Note: Set to empty to avoid creating an implicit stage, we'll create it
# explicitly below instead.
stage_name="")

# Create a stage, which is an addressable instance of the Rest API. Set it to point at the latest deployment.
stage = aws.apigateway.Stage("api-stage",
rest_api=rest_api,
deployment=deployment,
stage_name=custom_stage_name,
)
rest_api=rest_api,
deployment=deployment,
stage_name=custom_stage_name,
)

# Give permissions from API Gateway to invoke the Lambda
invoke_permission = aws.lambda_.Permission("api-lambda-permission",
action="lambda:invokeFunction",
function=lambda_func,
principal="apigateway.amazonaws.com",
source_arn=deployment.execution_arn.apply(
lambda execution_arn: execution_arn + "*/*"),
)
action="lambda:invokeFunction",
function=lambda_func,
principal="apigateway.amazonaws.com",
source_arn=deployment.execution_arn.apply(
lambda execution_arn: execution_arn + "*/*"),
)

# Export the https endpoint of the running Rest API
pulumi.export("endpoint", deployment.invoke_url.apply(lambda url: url + custom_stage_name))
3 changes: 3 additions & 0 deletions aws-ts-serverless-raw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**/obj
/**/bin
/**/node_modules
2 changes: 1 addition & 1 deletion aws-ts-serverless-raw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ to accomplish all of the same things this higher-level package offers.
The deployed Lambda function is a simple C# application, highlighting the ability to manage existing application code
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
The Lambda function is a C# application using .NET Core 3.1 (a similar approach works for any other language supported by
AWS Lambda).

## Deploying and running the Pulumi App
Expand Down

0 comments on commit 07d80a0

Please sign in to comment.