-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.tf
114 lines (100 loc) · 2.8 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
# Configure AWS Credentials & Region
provider "aws" {
profile = "${var.profile}"
region = "${var.region}"
}
# S3 Bucket for storing Elastic Beanstalk task definitions
resource "aws_s3_bucket" "ng_beanstalk_deploys" {
bucket = "${var.application_name}-deployments"
}
# Elastic Container Repository for Docker images
resource "aws_ecr_repository" "ng_container_repository" {
name = "${var.application_name}"
}
# Beanstalk instance profile
resource "aws_iam_instance_profile" "ng_beanstalk_ec2" {
name = "ng-beanstalk-ec2-user"
roles = ["${aws_iam_role.ng_beanstalk_ec2.name}"]
}
resource "aws_iam_role" "ng_beanstalk_ec2" {
name = "ng-beanstalk-ec2-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Beanstalk EC2 Policy
# Overriding because by default Beanstalk does not have a permission to Read ECR
resource "aws_iam_role_policy" "ng_beanstalk_ec2_policy" {
name = "ng_beanstalk_ec2_policy_with_ECR"
role = "${aws_iam_role.ng_beanstalk_ec2.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:PutMetricData",
"ds:CreateComputer",
"ds:DescribeDirectories",
"ec2:DescribeInstanceStatus",
"logs:*",
"ssm:*",
"ec2messages:*",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"s3:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
# Beanstalk Application
resource "aws_elastic_beanstalk_application" "ng_beanstalk_application" {
name = "${var.application_name}"
description = "${var.application_description}"
}
# Beanstalk Environment
resource "aws_elastic_beanstalk_environment" "ng_beanstalk_application_environment" {
name = "${var.application_name}-${var.application_environment}"
application = "${aws_elastic_beanstalk_application.ng_beanstalk_application.name}"
solution_stack_name = "64bit Amazon Linux 2016.09 v2.5.1 running Docker 1.12.6"
tier = "WebServer"
setting {
namespace = "aws:autoscaling:launchconfiguration"
name = "InstanceType"
# Todo: As Variable
value = "t2.micro"
}
setting {
namespace = "aws:autoscaling:asg"
name = "MaxSize"
# Todo: As Variable
value = "2"
}
setting {
namespace = "aws:autoscaling:launchconfiguration"
name = "IamInstanceProfile"
value = "${aws_iam_instance_profile.ng_beanstalk_ec2.name}"
}
}