Skip to content

docs: Add Idempotency example #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
638 changes: 638 additions & 0 deletions examples/Idempotency/.gitignore

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions examples/Idempotency/PowertoolsIdempotencyExample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "src\HelloWorld\HelloWorld.csproj", "{F36CE828-C7B1-4BD1-AC4B-500C7ACD14E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld.Tests", "test\HelloWorld.Test\HelloWorld.Tests.csproj", "{9B31B9EA-52BC-47B6-B78B-AC16502D8D4A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F36CE828-C7B1-4BD1-AC4B-500C7ACD14E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F36CE828-C7B1-4BD1-AC4B-500C7ACD14E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F36CE828-C7B1-4BD1-AC4B-500C7ACD14E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F36CE828-C7B1-4BD1-AC4B-500C7ACD14E1}.Release|Any CPU.Build.0 = Release|Any CPU
{9B31B9EA-52BC-47B6-B78B-AC16502D8D4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B31B9EA-52BC-47B6-B78B-AC16502D8D4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B31B9EA-52BC-47B6-B78B-AC16502D8D4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B31B9EA-52BC-47B6-B78B-AC16502D8D4A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
71 changes: 71 additions & 0 deletions examples/Idempotency/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Powertools for AWS Lambda (.NET) - Idempotency Example

This project contains source code and supporting files for a serverless application that you can deploy with the AWS Serverless Application Model Command Line Interface (AWS SAM CLI). It includes the following files and folders.

* src - Code for the application's Lambda function.
* events - Invocation events that you can use to invoke the function.
* test - Tests for the application code.
* template.yaml - A template that defines the application's AWS resources.

The application uses several AWS resources, including Lambda function, DynamoDB table and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.

If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. The AWS Toolkit is an open source plug-in for popular IDEs that uses the AWS SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.

* [Visual Studio Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)
* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)
* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)

## Deploy the sample application

The AWS SAM CLI is an extension of the AWS Command Line Interface (AWS CLI) that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.

To use the AWS SAM CLI, you need the following tools.

* AWS SAM CLI - [Install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)

You will need the following for local testing.
* .NET 6.0 - [Install .NET 6.0](https://www.microsoft.com/net/download)

To build and deploy your application for the first time, run the following in your shell. Make sure the `template.yaml` file is in your current directory:

```bash
sam build
sam deploy --guided
```

The first command will build a docker image from a Dockerfile and then copy the source of your application inside the Docker image. The second command will package and deploy your application to AWS, with a series of prompts:

* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
* **AWS Region**: The AWS region you want to deploy your app to.
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application.

You can find your API Gateway Endpoint URL in the output values displayed after deployment.

## Invoke Amazon API Gateway

Invoke the Lambda by calling the API Gateway using the following command

```bash
curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/hello/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}'
```

## Run tests

Tests use the [Testcontainers for .NET](https://dotnet.testcontainers.org/) which requires a Docker-API compatible container runtime.

Tests are defined in the `test` folder in this project.

```bash
$ dotnet test test/HelloWorld.Test
```

## Cleanup

To delete the sample application that you created, use the AWS SAM CLI. Assuming you used your project name for the stack name, you can run the following:

```bash
$ sam delete
```
63 changes: 63 additions & 0 deletions examples/Idempotency/events/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"body": "{\"address\": \"https://checkip.amazonaws.com\"}",
"resource": "/{proxy+}",
"path": "/path/to/resource",
"httpMethod": "POST",
"isBase64Encoded": false,
"queryStringParameters": {
"foo": "bar"
},
"pathParameters": {
"proxy": "/path/to/resource"
},
"stageVariables": {
"baz": "qux"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "123456",
"stage": "prod",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"requestTime": "09/Apr/2015:12:34:56 +0000",
"requestTimeEpoch": 1428582896000,
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"accessKey": null,
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Custom User Agent String",
"user": null
},
"path": "/prod/path/to/resource",
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "1234567890",
"protocol": "HTTP/1.1"
}
}

170 changes: 170 additions & 0 deletions examples/Idempotency/src/HelloWorld/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;
using AWS.Lambda.Powertools.Idempotency;
using AWS.Lambda.Powertools.Idempotency.Persistence;
using AWS.Lambda.Powertools.Logging;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]

namespace HelloWorld;

public class Function
{
private static HttpClient? _httpClient;
private static AmazonDynamoDBClient? _dynamoDbClient;

/// <summary>
/// Function constructor
/// </summary>
public Function()
{
_httpClient = new HttpClient();
_dynamoDbClient = new AmazonDynamoDBClient();

Init(_dynamoDbClient, _httpClient);
}

/// <summary>
/// Test constructor
/// </summary>
public Function(AmazonDynamoDBClient amazonDynamoDb, HttpClient httpClient)
{
_httpClient = httpClient;
_dynamoDbClient = amazonDynamoDb;
Init(amazonDynamoDb, httpClient);
}

private void Init(AmazonDynamoDBClient amazonDynamoDb, HttpClient httpClient)
{
ArgumentNullException.ThrowIfNull(amazonDynamoDb);
ArgumentNullException.ThrowIfNull(httpClient);
var tableName = Environment.GetEnvironmentVariable("TABLE_NAME");
ArgumentNullException.ThrowIfNull(tableName);

Idempotency.Configure(builder =>
builder
.WithOptions(optionsBuilder =>
optionsBuilder
.WithEventKeyJmesPath("powertools_json(Body).address") // will retrieve the address field in the body which is a string transformed to json with `powertools_json`
.WithExpiration(TimeSpan.FromSeconds(10)))
.UseDynamoDb(storeBuilder =>
storeBuilder
.WithTableName(tableName)
.WithDynamoDBClient(amazonDynamoDb)
));
}

/// <summary>
/// Lambda Handler
/// Try with:
/// <pre>
/// curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/hello/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}'
/// </pre>
/// </summary>
/// <param name="apigwProxyEvent">API Gateway Proxy event</param>
/// <param name="context">AWS Lambda context</param>
/// <returns>API Gateway Proxy response</returns>
[Idempotent]
[Logging(LogEvent = true)]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
{
var serializationOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var request = JsonSerializer.Deserialize<LookupRequest>(apigwProxyEvent.Body, serializationOptions);
if (request is null)
{
return new APIGatewayProxyResponse
{
Body = "Invalid request",
StatusCode = 403,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}

var location = await GetCallingIp(request.Address);

var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
var response = new
{
RequestId = requestContextRequestId,
Greeting = "Hello Powertools for AWS Lambda (.NET)",
IpAddress = location
};

try
{
return new APIGatewayProxyResponse
{
Body = JsonSerializer.Serialize(response),
StatusCode = 200,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}
catch (Exception e)
{
return new APIGatewayProxyResponse
{
Body = e.Message,
StatusCode = 500,
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
}
}

/// <summary>
/// Calls location api to return IP address
/// </summary>
/// <param name="address">Uri of the service providing the calling IP</param>
/// <returns>IP address string</returns>
private static async Task<string?> GetCallingIp(string address)
{
if (_httpClient == null) return "0.0.0.0";
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client");

var response = await _httpClient.GetStringAsync(address).ConfigureAwait(false);
var ip = response.Replace("\n", "");

return ip;
}
}

/// <summary>
/// Record to represent the data structure of Lookup request
/// </summary>
[Serializable]
public class LookupRequest
{
public string Address { get; private set; }

public LookupRequest(string address)
{
Address = address;
}
}
15 changes: 15 additions & 0 deletions examples/Idempotency/src/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWS.Lambda.Powertools.Idempotency" Version="0.0.1-preview" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions examples/Idempotency/src/HelloWorld/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "",
"region": "",
"configuration": "Release",
"template": "template.yaml"
}
Loading