-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
104 lines (87 loc) · 2.15 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
### lambda - start ###
resource "aws_lambda_function" "create-ami" {
function_name = "create-ami"
s3_bucket = "${aws_s3_bucket.lambda-source.id}"
s3_key = "create-ami-v0.1.zip"
role = "${aws_iam_role.lambda-create-ami.arn}"
handler = "main.handle"
runtime = "python2.7"
timeout = 30
tags {
Name = "create-ami"
Terraform = "true"
}
}
resource "aws_lambda_permission" "create-ami" {
statement_id = "create-ami-AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "create-ami"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.create-ami.arn}"
depends_on = ["aws_lambda_function.create-ami"]
}
### lambda - end ###
### s3 - start ###
resource "aws_s3_bucket" "lambda-source" {
bucket = "${var.s3-bucket-lambda-source}"
force_destroy = false
versioning {
enabled = true
}
tags {
Name = "${var.s3-bucket-lambda-source}"
Terraform = "true"
}
}
### s3 - end ###
### cloudwatch - start ###
resource "aws_cloudwatch_event_rule" "create-ami" {
name = "create-ami-schedule"
schedule_expression = "${var.create-ami-schedule}"
}
resource "aws_cloudwatch_event_target" "create-ami" {
rule = "${aws_cloudwatch_event_rule.create-ami.name}"
arn = "${aws_lambda_function.create-ami.arn}"
}
### cloudwatch - end ###
### iam - start ###
resource "aws_iam_role" "lambda-create-ami" {
name = "lambda-create-ami"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "lambda-create-ami" {
name = "lambda-create-ami"
role = "${aws_iam_role.lambda-create-ami.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"ec2:*"
],
"Resource": "*"
}
]
}
EOF
}
### iam - end ###