Skip to content

Commit 63bbf85

Browse files
authored
Merge pull request aws-samples#1644 from aws-samples/SoufanConsulting
Soufan consulting
2 parents eca7835 + 04bd338 commit 63bbf85

23 files changed

Lines changed: 643 additions & 0 deletions
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
![Architecture](assets/Lambda-elastic-ip-no-nat-gateway.svg)
7+
## Production Architecture
8+
![Production Architecture](assets/Lambda-elastic-ip-with-nat.svg)
9+
## Non-prod cost effective Architecture
10+
![Non-prod cost effective Architecture](assets/Lambda-elastic-ip-with-x-nat-gateway.svg)
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
58.1 KB
Loading

lambda-elastic-ip-no-nat-gateway-cdk/assets/Lambda-Elastic-IP.svg

Lines changed: 1 addition & 0 deletions
Loading

lambda-elastic-ip-no-nat-gateway-cdk/assets/Lambda-elastic-ip-no-nat-gateway.svg

Lines changed: 1 addition & 0 deletions
Loading

lambda-elastic-ip-no-nat-gateway-cdk/assets/Lambda-elastic-ip-with-nat.svg

Lines changed: 1 addition & 0 deletions
Loading

lambda-elastic-ip-no-nat-gateway-cdk/assets/Lambda-elastic-ip-with-x-nat-gateway.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
9+
10+
template.yaml
11+
cdk.context.json
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Welcome to your CDK TypeScript project
2+
3+
This is a blank project for CDK development with TypeScript.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
## Useful commands
8+
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npm run test` perform the jest unit tests
12+
* `cdk deploy` deploy this stack to your default AWS account/region
13+
* `cdk diff` compare deployed stack with current state
14+
* `cdk synth` emits the synthesized CloudFormation template
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { LambdaElasticIpStack } from '../lib/cdk-stack';
5+
6+
const app = new cdk.App();
7+
8+
const patternStack = new LambdaElasticIpStack(app, 'LambdaElasticIpStack', {
9+
env: {
10+
region: process.env.CDK_DEFAULT_REGION,
11+
account: process.env.CDK_DEFAULT_ACCOUNT,
12+
},
13+
});
14+
15+
app.synth();

0 commit comments

Comments
 (0)