Skip to content

Instantly share code, notes, and snippets.

@sans-clue
Last active March 20, 2024 16:50
Show Gist options
  • Save sans-clue/35847188fd165cb3768bcf363f7eabca to your computer and use it in GitHub Desktop.
Save sans-clue/35847188fd165cb3768bcf363f7eabca to your computer and use it in GitHub Desktop.
Serverless Framework - VPC - Static IP
/* global fetch */
const serverless = require("serverless-http");
const express = require("express");
const app = express();
app.use(async (req, res, next) => {
const response = await fetch("https://api.ipify.org");
const ip = await response.text();
res.locals.ip_addr = ip;
next();
});
app.get("/", (req, res, next) => {
return res.status(200).json({
message: "Hello from root!",
ip: res.locals.ip_addr,
});
});
app.get("/path", (req, res, next) => {
return res.status(200).json({
message: "Hello from path!",
ip: res.locals.ip_addr,
});
});
app.use((req, res, next) => {
return res.status(404).json({
error: "Not Found",
});
});
module.exports.handler = serverless(app);
service: vpc-test
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs18.x
functions:
api:
handler: index.handler
events:
- httpApi: "*"
vpc:
securityGroupIds:
- !Ref MyLambdaSecurityGroup
subnetIds:
- !Ref PublicSubnet
- !Ref PrivateSubnet
custom:
customDomain:
domainName: "test.domain.com"
basePath: v1
endpointType: regional
apiType: http
createRoute53Record: true
resources:
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
# Public subnet definition
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: us-east-1a
# Private subnet definition
PrivateSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.2.0/24
AvailabilityZone: us-east-1a
# Internet Gateway definition
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
# VPC Gateway Attachment
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
# Security Group definition for Lambda function
MyLambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: proxy-sg-${sls:stage}
GroupDescription: Allow http traffic through
VpcId: !Ref VPC
SecurityGroupIngress:
- FromPort: 80
ToPort: 80
IpProtocol: tcp
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- FromPort: 80
ToPort: 80
IpProtocol: tcp
CidrIp: 0.0.0.0/0
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PublicRoute:
Type: AWS::EC2::Route
DependsOn: VPCGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
# NAT Gateway definition with Elastic IP allocation
NatGateway:
Type: AWS::EC2::NatGateway
Properties:
SubnetId: !Ref PublicSubnet
AllocationId: !GetAtt ElasticIp.AllocationId
# Elastic IP definition
ElasticIp:
Type: AWS::EC2::EIP
Properties:
Outputs:
NatGatewayPublicIp:
Value: !GetAtt ElasticIp.PublicIp
plugins:
- serverless-domain-manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment