forked from FormidableLabs/aws-lambda-serverless-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
172 lines (148 loc) · 5.36 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
provider "aws" {
region = "${var.region}"
}
terraform {
backend "s3" {
key = "terraform.tfstate"
}
}
###############################################################################
# Base `serverless` IAM support.
###############################################################################
module "serverless" {
source = "FormidableLabs/serverless/aws"
region = "${var.region}"
service_name = "${var.service_name}"
stage = "${var.stage}"
# (Default values)
# iam_region = `*`
# iam_partition = `*`
# iam_account_id = `AWS_CALLER account`
# tf_service_name = `tf-SERVICE_NAME`
# sls_service_name = `sls-SERVICE_NAME`
# role_admin_name = `admin`
# role_developer_name = `developer`
# role_ci_name = `ci`
# opt_many_lambdas = false
}
###############################################################################
# OPTION(Xray): Add X-ray support to lambda execution roles.
###############################################################################
module "serverless_xray" {
source = "FormidableLabs/serverless/aws//modules/xray"
# Same variables as for `serverless` module.
region = "${var.region}"
service_name = "${var.service_name}"
stage = "${var.stage}"
}
###############################################################################
# OPTION(VPC): Create VPC resources and expose to Serverless stack.
###############################################################################
data "aws_availability_zones" "available" {}
# OPTION(VPC): Instantiate an actual VPC
#
# ## Available ranges
#
# - 10.0.0.0 - 10.255.255.255 (10/8 prefix)
# - 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
# - 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
#
# - `10.0.0.0/8` is reserved for ClassicLink VPC. Don't use if need that.
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/vpc-classiclink.html
# - `172.31.0.0/16` is usually the default VPC group.
#
# ## Addressing
#
# Param CIDR Start End Hosts
# ======================= =============== =========== ============= ======
# Private Subnet A 10.1.0.0/20 10.1.0.0 10.1.15.255 4096
# Private Subnet B 10.1.16.0/20 10.1.16.0 10.1.31.255 4096
# <Private Spare> C 10.1.32.0/20
# <Private Spare> D 10.1.48.0/20
#
# Public Subnet A 10.1.64.0/20 10.1.64.0 10.1.79.255 4096
# Public Subnet B 10.1.80.0/20 10.1.80.0 10.1.95.255 4096
# <Public Spare> C 10.1.96.0/20
# <Public Spare> D 10.1.112.0/20
#
# VPC CIDR Block 10.1.0.0/17 10.1.0.0 10.1.127.255 32768
module "vpc" "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "1.66.0"
name = "tf-${var.service_name}-${var.stage}"
# Dynamically get 2 availabile AZs for failover.
azs = [
"${data.aws_availability_zones.available.names[0]}",
"${data.aws_availability_zones.available.names[1]}",
]
# Features
enable_dns_hostnames = true
enable_dns_support = true
enable_nat_gateway = true
# Networking
cidr = "10.1.0.0/17"
private_subnets = ["10.1.0.0/20", "10.1.16.0/20"]
public_subnets = ["10.1.64.0/20", "10.1.80.0/20"]
tags = "${local.tags}"
}
# OPTION(VPC): Use a custom, honed SG.
resource "aws_security_group" "vpc" {
name = "tf-${var.service_name}-${var.stage}"
description = "Allow Serverless Lambda networking"
vpc_id = "${module.vpc.vpc_id}"
egress {
description = "Egress: tf-${var.service_name}-${var.stage}"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = "${merge(local.tags, map(
"Name", "tf-${var.service_name}-${var.stage}",
))}"
}
# OPTION(VPC): Use a small CloudFormation stack to expose outputs for
# consumption in Serverless. (There are _many_ ways to do this, we just
# like this as there's no local disk state needed to deploy.)
#
# _Note_: CF **requires** 1+ `Resources`, so we throw in the SSM param of the
# VPC SG because it's small and we need "something". It's otherwise unused.
#
# See: https://theburningmonk.com/2019/03/making-terraform-and-serverless-framework-work-together/
resource "aws_cloudformation_stack" "outputs" {
name = "tf-${var.service_name}-${var.stage}-outputs"
template_body = <<STACK
Resources:
VPCSecurityGroupId:
Type: AWS::SSM::Parameter
Properties:
Name: "tf-${var.service_name}-${var.stage}-VPCSecurityGroupId"
Value: "${aws_security_group.vpc.id}"
Type: String
Outputs:
VPCSecurityGroupId:
Description: "VPC SG GID"
Value: "${aws_security_group.vpc.id}"
Export:
Name: "tf-${var.service_name}-${var.stage}-VPCSecurityGroupId"
VPCPrivateSubnetA:
Description: "VPC Private Subnet A"
Value: "${module.vpc.private_subnets[0]}"
Export:
Name: "tf-${var.service_name}-${var.stage}-VPCPrivateSubnetA"
VPCPrivateSubnetB:
Description: "VPC Private Subnet B"
Value: "${module.vpc.private_subnets[1]}"
Export:
Name: "tf-${var.service_name}-${var.stage}-VPCPrivateSubnetB"
STACK
tags = "${local.tags}"
}
# OPTION(VPC): Add in IAM permissions to humans + lambda execution role.
module "serverless_vpc" {
source = "FormidableLabs/serverless/aws//modules/vpc"
# Same variables as for `serverless` module.
region = "${var.region}"
service_name = "${var.service_name}"
stage = "${var.stage}"
}