Skip to content

Commit 2900873

Browse files
committed
added logging to lambda
1 parent 5527d43 commit 2900873

File tree

1 file changed

+81
-48
lines changed

1 file changed

+81
-48
lines changed

main.tf

Lines changed: 81 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,85 @@ module "acs" {
55
vpc_vpn_to_campus = true
66
}
77

8+
resource "aws_cloudwatch_log_group" "logs" {
9+
name = "/aws/lambda/${aws_lambda_function.lambda.function_name}"
10+
retention_in_days = 14
11+
}
12+
13+
resource "aws_iam_role" "iam_for_lambda" {
14+
name = "${var.app-name}-lambda"
15+
permissions_boundary = "arn:aws:iam::${var.account-id}:policy/iamRolePermissionBoundary"
16+
assume_role_policy = <<EOF
17+
{
18+
"Version": "2012-10-17",
19+
"Statement": [
20+
{
21+
"Action": "sts:AssumeRole",
22+
"Principal": {
23+
"Service": "lambda.amazonaws.com"
24+
},
25+
"Effect": "Allow",
26+
"Sid": ""
27+
}
28+
]
29+
}
30+
EOF
31+
}
32+
33+
resource aws_iam_policy "ec2-network-interface-policy" {
34+
name = "${var.app-name}-ec2"
35+
description = "A policy to allow create, describe, and delete network interfaces"
36+
37+
policy = <<EOF
38+
{
39+
"Version": "2012-10-17",
40+
"Statement": [
41+
{
42+
"Effect": "Allow",
43+
"Action": [
44+
"ec2:CreateNetworkInterface",
45+
"ec2:DescribeNetworkInterfaces",
46+
"ec2:DeleteNetworkInterface"
47+
],
48+
"Resource": "*"
49+
}
50+
]
51+
}
52+
EOF
53+
}
54+
55+
resource "aws_iam_role_policy_attachment" "ec2-network-interface-policy-attachment" {
56+
role = aws_iam_role.iam_for_lambda.name
57+
policy_arn = aws_iam_policy.ec2-network-interface-policy.arn
58+
}
59+
60+
resource "aws_iam_policy" "lambda_logging" {
61+
name = "${var.app-name}-lambda-logging"
62+
description = "IAM policy for logging from a lambda"
63+
64+
policy = <<EOF
65+
{
66+
"Version": "2012-10-17",
67+
"Statement": [
68+
{
69+
"Action": [
70+
"logs:CreateLogGroup",
71+
"logs:CreateLogStream",
72+
"logs:PutLogEvents"
73+
],
74+
"Resource": "arn:aws:logs:*:*:*",
75+
"Effect": "Allow"
76+
}
77+
]
78+
}
79+
EOF
80+
}
81+
82+
resource "aws_iam_role_policy_attachment" "lambda_logs" {
83+
role = aws_iam_role.iam_for_lambda.name
84+
policy_arn = aws_iam_policy.lambda_logging.arn
85+
}
86+
887
resource "aws_api_gateway_rest_api" "api" {
988
name = "${var.app-name}-api"
1089
}
@@ -69,8 +148,9 @@ resource "aws_lambda_permission" "apigw_lambda" {
69148
action = "lambda:InvokeFunction"
70149
function_name = aws_lambda_function.lambda.function_name
71150
principal = "apigateway.amazonaws.com"
72-
73151
source_arn = "arn:aws:execute-api:us-west-2:${var.account-id}:${aws_api_gateway_rest_api.api.id}/*"
152+
depends_on = [aws_iam_role_policy_attachment.lambda_logs, aws_cloudwatch_log_group.logs]
153+
74154
}
75155

76156
resource "aws_api_gateway_domain_name" "api_domain" {
@@ -100,53 +180,6 @@ resource "aws_route53_record" "a_record" {
100180
}
101181
}
102182

103-
resource "aws_iam_role" "iam_for_lambda" {
104-
name = "${var.app-name}-lambda"
105-
permissions_boundary = "arn:aws:iam::${var.account-id}:policy/iamRolePermissionBoundary"
106-
assume_role_policy = <<EOF
107-
{
108-
"Version": "2012-10-17",
109-
"Statement": [
110-
{
111-
"Action": "sts:AssumeRole",
112-
"Principal": {
113-
"Service": "lambda.amazonaws.com"
114-
},
115-
"Effect": "Allow",
116-
"Sid": ""
117-
}
118-
]
119-
}
120-
EOF
121-
}
122-
123-
resource aws_iam_policy "ec2-network-interface-policy" {
124-
name = "${var.app-name}-ec2"
125-
description = "A policy to allow create, describe, and delete network interfaces"
126-
127-
policy = <<EOF
128-
{
129-
"Version": "2012-10-17",
130-
"Statement": [
131-
{
132-
"Effect": "Allow",
133-
"Action": [
134-
"ec2:CreateNetworkInterface",
135-
"ec2:DescribeNetworkInterfaces",
136-
"ec2:DeleteNetworkInterface"
137-
],
138-
"Resource": "*"
139-
}
140-
]
141-
}
142-
EOF
143-
}
144-
145-
resource "aws_iam_role_policy_attachment" "ec2-network-interface-policy-attachment" {
146-
role = aws_iam_role.iam_for_lambda.name
147-
policy_arn = aws_iam_policy.ec2-network-interface-policy.arn
148-
}
149-
150183
resource "aws_security_group" "vpc_sec" {
151184
name = "${var.app-name}-sg"
152185
description = "${var.app-name}-sg"

0 commit comments

Comments
 (0)