Permalink
Switch branches/tags
Nothing to show
Find file Copy path
ea9cbe6 Mar 17, 2017
1 contributor

Users who have contributed to this file

115 lines (100 sloc) 2.8 KB
# 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}"
}
}