Skip to content

Commit 6d550b6

Browse files
authored
feat: Allow Lambda function to be VPC bound (#122)
1 parent d56b7cd commit 6d550b6

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
terraform.tfstate
33
*.tfstate*
44
terraform.tfvars
5+
.terraform.lock.hcl
56

67
builds/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ To run the tests:
109109
| kms\_key\_arn | ARN of the KMS key used for decrypting slack webhook url | `string` | `""` | no |
110110
| lambda\_description | The description of the Lambda function | `string` | `null` | no |
111111
| lambda\_function\_name | The name of the Lambda function to create | `string` | `"notify_slack"` | no |
112+
| lambda\_function\_s3\_bucket | S3 bucket to store artifacts | `string` | `null` | no |
113+
| lambda\_function\_store\_on\_s3 | Whether to store produced artifacts on S3 or locally. | `bool` | `false` | no |
112114
| lambda\_function\_tags | Additional tags for the Lambda function | `map(string)` | `{}` | no |
115+
| lambda\_function\_vpc\_security\_group\_ids | List of security group ids when Lambda Function should run in the VPC. | `list(string)` | `null` | no |
116+
| lambda\_function\_vpc\_subnet\_ids | List of subnet ids when Lambda Function should run in the VPC. Usually private or intra subnets. | `list(string)` | `null` | no |
113117
| lambda\_role | IAM role attached to the Lambda Function. If this is set then a role will not be created for you. | `string` | `""` | no |
114118
| log\_events | Boolean flag to enabled/disable logging of incoming events | `bool` | `false` | no |
115119
| reserved\_concurrent\_executions | The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations | `number` | `-1` | no |

examples/cloudwatch-alerts-to-slack/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CloudWatch alerts to Slack
22

3-
Configuration in this directory creates an SNS topic that sends messages to a Slack channel with Slack webhook URL encrypted using KMS and a CloudWatch Alarm that monitors the duration of lambda execution.
3+
Configuration in this directory creates a VPC, an SNS topic that sends messages to a Slack channel with Slack webhook URL encrypted using KMS and a CloudWatch Alarm that monitors the duration of lambda execution.
44

55
## KMS keys
66

@@ -62,12 +62,14 @@ Note that this example may create resources which can cost money. Run `terraform
6262
|------|---------|
6363
| terraform | >= 0.13.0 |
6464
| aws | >= 2.35 |
65+
| random | >= 2 |
6566

6667
## Providers
6768

6869
| Name | Version |
6970
|------|---------|
7071
| aws | >= 2.35 |
72+
| random | >= 2 |
7173

7274
## Inputs
7375

examples/cloudwatch-alerts-to-slack/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ module "notify_slack" {
3535
lambda_description = "Lambda function which sends notifications to Slack"
3636
log_events = true
3737

38+
# VPC
39+
# lambda_function_vpc_subnet_ids = module.vpc.intra_subnets
40+
# lambda_function_vpc_security_group_ids = [module.vpc.default_security_group_id]
41+
3842
tags = {
3943
Name = "cloudwatch-alerts-to-slack"
4044
}
@@ -57,3 +61,20 @@ resource "aws_cloudwatch_metric_alarm" "lambda_duration" {
5761
FunctionName = module.notify_slack["develop"].notify_slack_lambda_function_name
5862
}
5963
}
64+
65+
######
66+
# VPC
67+
######
68+
resource "random_pet" "this" {
69+
length = 2
70+
}
71+
72+
module "vpc" {
73+
source = "terraform-aws-modules/vpc/aws"
74+
75+
name = random_pet.this.id
76+
cidr = "10.10.0.0/16"
77+
78+
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
79+
intra_subnets = ["10.10.101.0/24", "10.10.102.0/24", "10.10.103.0/24"]
80+
}

examples/cloudwatch-alerts-to-slack/versions.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ terraform {
22
required_version = ">= 0.13.0"
33

44
required_providers {
5-
aws = ">= 2.35"
5+
aws = ">= 2.35"
6+
random = ">= 2"
67
}
78
}

main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module "lambda" {
106106
policy_json = element(concat(data.aws_iam_policy_document.lambda[*].json, [""]), 0)
107107

108108
use_existing_cloudwatch_log_group = true
109+
attach_network_policy = var.lambda_function_vpc_subnet_ids != null
109110

110111
allowed_triggers = {
111112
AllowExecutionFromSNS = {
@@ -114,6 +115,12 @@ module "lambda" {
114115
}
115116
}
116117

118+
store_on_s3 = var.lambda_function_store_on_s3
119+
s3_bucket = var.lambda_function_s3_bucket
120+
121+
vpc_subnet_ids = var.lambda_function_vpc_subnet_ids
122+
vpc_security_group_ids = var.lambda_function_vpc_security_group_ids
123+
117124
tags = merge(var.tags, var.lambda_function_tags)
118125

119126
depends_on = [aws_cloudwatch_log_group.lambda]

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ variable "lambda_function_tags" {
120120
default = {}
121121
}
122122

123+
variable "lambda_function_vpc_subnet_ids" {
124+
description = "List of subnet ids when Lambda Function should run in the VPC. Usually private or intra subnets."
125+
type = list(string)
126+
default = null
127+
}
128+
129+
variable "lambda_function_vpc_security_group_ids" {
130+
description = "List of security group ids when Lambda Function should run in the VPC."
131+
type = list(string)
132+
default = null
133+
}
134+
135+
variable "lambda_function_store_on_s3" {
136+
description = "Whether to store produced artifacts on S3 or locally."
137+
type = bool
138+
default = false
139+
}
140+
141+
variable "lambda_function_s3_bucket" {
142+
description = "S3 bucket to store artifacts"
143+
type = string
144+
default = null
145+
}
146+
123147
variable "sns_topic_tags" {
124148
description = "Additional tags for the SNS topic"
125149
type = map(string)

0 commit comments

Comments
 (0)