|
| 1 | +# AWS Lambda Elastic IP without NAT Gateway |
| 2 | + |
| 3 | +This project contains a sample AWS Cloud Development Kit (AWS CDK) template for deploying a Lambda function with a public elastic IP that has internet access without the need to provision a NAT gateway. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +## Production Architecture |
| 8 | + |
| 9 | +## Non-prod cost effective Architecture |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-elastic-ip-no-nat-gateway-cdk. |
| 14 | + |
| 15 | +Important: This application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 20 | +- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 21 | +- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 22 | +- [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) installed and configured |
| 23 | + |
| 24 | +## Deployment Instructions |
| 25 | + |
| 26 | +1. Create a new directory, navigate to that directory in a terminal, and clone the GitHub repository: |
| 27 | + ```bash |
| 28 | + git clone https://github.com/aws-samples/serverless-patterns |
| 29 | + ``` |
| 30 | +2. Change the directory to the pattern directory: |
| 31 | + ```bash |
| 32 | + cd serverless-patterns/lambda-elastic-ip-no-nat-gateway-cdk/cdk |
| 33 | + ``` |
| 34 | +3. Install dependencies for both the infrastructure project and the typescript project: |
| 35 | + ```bash |
| 36 | + npm install |
| 37 | + cd src |
| 38 | + npm install |
| 39 | + cd .. |
| 40 | + ``` |
| 41 | + |
| 42 | +4. From the command line, configure AWS CDK: |
| 43 | + ```bash |
| 44 | + cdk bootstrap ACCOUNT-NUMBER/REGION # e.g. |
| 45 | + cdk bootstrap 9999999999/us-east-1 |
| 46 | + cdk bootstrap --profile test 9999999999/us-east-1 |
| 47 | + ``` |
| 48 | +5. From the command line, use AWS CDK to deploy the AWS resources for the pattern as specified in the `lib/cdk-stack.ts` file: |
| 49 | + ```bash |
| 50 | + cdk deploy |
| 51 | + ``` |
| 52 | + |
| 53 | +## Use Case |
| 54 | +You have a Lambda function that requires internet access to make API calls to 3rd party service but you need a dedicated IP to be whitelisted by the 3rd party vendors. |
| 55 | + |
| 56 | +## How it works |
| 57 | + |
| 58 | +This pattern allows you to assign your Lambda function a static public IP address that you can use to interact with APIs that require whitelisted IPs without the need to provision a NAT Gateway. Therefore, this pattern will save almost **$33/month** in NAT Gateway costs. |
| 59 | + |
| 60 | +##### **NOTE:** This pattern is best suited for non-production environments since it is not multi-AZ nor highly scalable. |
| 61 | + |
| 62 | +The following resources will be provisioned: |
| 63 | + |
| 64 | +- A Lambda function to test the pattern |
| 65 | +- An Elastic IP to associate with the Lambda function |
| 66 | +- A custom resource with Lambda function to associate the Elastic IP with the test lambda's ENI |
| 67 | + |
| 68 | +Since AWS manages the provisioning of any Lambda ENI, we cannot access that ENI in CDK code. Therefore, to automate the process, we have to associate the Elastic IP with the ENI in a custom resource after the deployment occurs and the ENI is provisioned. |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +To test this pattern, use the AWS Console or the AWS CLI. |
| 73 | + |
| 74 | +### AWS Console Part |
| 75 | + |
| 76 | +1. Open the AWS Lambda Console |
| 77 | +2. Navigate to `vin-api-lambda` |
| 78 | +3. Test the lambda with any payload |
| 79 | +4. The Lambda function shouldn't time out and a random vin should be returned and logged. |
| 80 | + |
| 81 | +## Cleanup |
| 82 | + |
| 83 | +1. Delete the stack |
| 84 | + ```bash |
| 85 | + cdk destroy |
| 86 | + ``` |
| 87 | + |
| 88 | +## Resources |
| 89 | + |
| 90 | +1. [Lambda in a VPC](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/generate-a-static-outbound-ip-address-using-a-lambda-function-amazon-vpc-and-a-serverless-architecture.html) |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 95 | + |
| 96 | +SPDX-License-Identifier: MIT-0 |
0 commit comments