-
Notifications
You must be signed in to change notification settings - Fork 2
BENCH-297: Terraform AWS infrastructure #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelOSullivanAnswer
merged 16 commits into
develop
from
BENCH-297-terraform-aws-infrastructure
Feb 1, 2023
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
545d1c0
initial commit
MichaelOSullivanAnswer 29fa733
Update README
MichaelOSullivanAnswer ac6f187
Update README
MichaelOSullivanAnswer da8cb74
Update README
MichaelOSullivanAnswer f567473
Update README
MichaelOSullivanAnswer d3dd63d
add variable for port in db security group and edit README
MichaelOSullivanAnswer efc6939
update terraform
MichaelOSullivanAnswer 5a3a4c9
variables for host and container port
MichaelOSullivanAnswer 99f53e2
change to use gunicorn
MichaelOSullivanAnswer 986caee
add backend.tf
MichaelOSullivanAnswer 4459d17
configure to be used with deploy github action
MichaelOSullivanAnswer ad8de7d
test
MichaelOSullivanAnswer d661e7b
re install gunicorn
MichaelOSullivanAnswer 99dccd5
setup s3 backend
MichaelOSullivanAnswer 31d39fd
setup s3 backend
MichaelOSullivanAnswer 65ccd84
update providers with versions
MichaelOSullivanAnswer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
terraform { | ||
backend "s3" { | ||
bucket = "answerking-python-terraform" | ||
key = "answerking-python-terraform.tfstate" | ||
region = "eu-west-2" | ||
dynamodb_table = "ak-python-terraform-state" | ||
} | ||
} | ||
|
||
/* | ||
|
||
# The commented out code was used to create the s3 bucket and dynamo table. | ||
# It has been removed from the state so will not be impacted by terraform destroy. | ||
|
||
resource "aws_s3_bucket" "terraform_backend_bucket" { | ||
bucket = "answerking-python-terraform" | ||
tags = { | ||
Name = "${var.project_name}-terraform-bucket" | ||
Owner = var.owner | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket_acl" "terraform_backend_bucket_acl" { | ||
bucket = aws_s3_bucket.terraform_backend_bucket.id | ||
acl = "private" | ||
|
||
} | ||
|
||
resource "aws_s3_bucket_public_access_block" "terraform_backend_bucket_public_access_block" { | ||
bucket = aws_s3_bucket.terraform_backend_bucket.id | ||
block_public_acls = true | ||
block_public_policy = true | ||
ignore_public_acls = true | ||
restrict_public_buckets = true | ||
} | ||
|
||
resource "aws_s3_bucket_versioning" "terraform_backend_bucket_versioning" { | ||
bucket = aws_s3_bucket.terraform_backend_bucket.id | ||
versioning_configuration { | ||
status = "Enabled" | ||
} | ||
} | ||
resource "aws_dynamodb_table" "terraform_backend_state" { | ||
name = "${var.project_name}-terraform-state" | ||
read_capacity = 20 | ||
write_capacity = 20 | ||
hash_key = "LockID" | ||
|
||
attribute { | ||
name = "LockID" | ||
type = "S" | ||
} | ||
|
||
tags = { | ||
Name = "${var.project_name}-dynamo-table" | ||
Owner = var.owner | ||
} | ||
} | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
resource "aws_ecs_cluster" "ecs_cluster" { | ||
name = "${var.project_name}-ecs-cluster" | ||
|
||
tags = { | ||
Name = "${var.project_name}-ecs" | ||
Owner = var.owner | ||
} | ||
} | ||
|
||
resource "aws_ecs_service" "service" { | ||
depends_on = [ | ||
module.rds_serverless_cluster_setup.rds_cluster_instance_endpoint, | ||
aws_iam_role.ecs_task_execution_role | ||
] | ||
name = "${var.project_name}-ecs-service" | ||
cluster = aws_ecs_cluster.ecs_cluster.id | ||
task_definition = aws_ecs_task_definition.task_definition.arn | ||
desired_count = 1 | ||
launch_type = var.service_launch_type | ||
scheduling_strategy = var.scheduling_strategy | ||
|
||
network_configuration { | ||
security_groups = [aws_security_group.ecs_sg.id] | ||
subnets = [module.vpc_subnet_setup.public_subnet_ids[0]] | ||
assign_public_ip = true | ||
} | ||
|
||
tags = { | ||
Name = "${var.project_name}-service" | ||
Owner = var.owner | ||
} | ||
} | ||
|
||
resource "aws_ecs_task_definition" "task_definition" { | ||
family = "${var.project_name}-task" | ||
network_mode = var.network_mode | ||
requires_compatibilities = [var.service_launch_type] | ||
cpu = var.ecs_task_cpu | ||
memory = var.ecs_task_memory | ||
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn | ||
container_definitions = jsonencode([ | ||
{ | ||
name = "${var.project_name}-container" | ||
image = "${var.image_url}" | ||
cpu = 256 | ||
essential = true | ||
networkMode = var.network_mode | ||
portMappings = [ | ||
{ | ||
containerPort = "${var.container_port}" | ||
hostPort = "${var.host_port}" | ||
}] | ||
logConfiguration = { | ||
logDriver = "awslogs", | ||
options = { | ||
"awslogs-group": "${var.project_name}-log-group", | ||
"awslogs-region": var.aws_region, | ||
"awslogs-stream-prefix": "${var.project_name}-log-stream" | ||
} | ||
} | ||
environment = [ | ||
{"name": "DATABASE_NAME", "value": var.database_name}, | ||
{"name": "DATABASE_HOST", "value": module.rds_serverless_cluster_setup.rds_cluster_instance_endpoint}, | ||
{"name": "DATABASE_PORT", "value": var.database_port}, | ||
{"name": "DATABASE_USER", "value": module.rds_serverless_cluster_setup.rds_cluster_master_username}, | ||
{"name": "DATABASE_PASS", "value": module.rds_serverless_cluster_setup.rds_cluster_master_password}, | ||
{"name": "SECRET_KEY", "value": var.django_secret_key}, | ||
{"name": "DJANGO_SETTINGS_MODULE", "value": var.django_settings_module}, | ||
{"name": "DATABASE_ENGINE", "value": var.django_database_engine} | ||
|
||
] | ||
} | ||
]) | ||
|
||
tags = { | ||
Name = "${var.project_name}-task-definition" | ||
Owner = var.owner | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
resource "aws_iam_role" "ecs_task_execution_role" { | ||
name = "ecsTaskExecutionRole-python" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "ecs-tasks.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecs-task-execution-role-policy-attachment" { | ||
role = aws_iam_role.ecs_task_execution_role.name | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
resource "aws_cloudwatch_log_group" "log-group" { | ||
name = "${var.project_name}-log-group" | ||
retention_in_days = 1 | ||
} | ||
|
||
resource "aws_cloudwatch_log_stream" "log-stream" { | ||
name = "${var.project_name}-log-stream" | ||
log_group_name = aws_cloudwatch_log_group.log-group.name | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
provider "aws" { | ||
region = "eu-west-2" | ||
skip_credentials_validation = true | ||
} | ||
|
||
module "vpc_subnet_setup" { | ||
source = "git::https://github.com/AnswerConsulting/AnswerKing-Infrastructure.git//Terraform_modules/vpc_subnets?ref=v1.0.0" | ||
project_name = var.project_name | ||
owner = var.owner | ||
num_public_subnets = 1 | ||
num_private_subnets = 2 | ||
} | ||
|
||
module "rds_serverless_cluster_setup" { | ||
source = "git::https://github.com/AnswerConsulting/AnswerKing-Infrastructure.git//Terraform_modules/rds_serverless_cluster?ref=v1.0.0" | ||
project_name = var.project_name | ||
owner = var.owner | ||
database_name = var.database_name | ||
database_engine = var.database_engine | ||
database_engine_version = var.database_engine_version | ||
database_subnet_ids = module.vpc_subnet_setup.private_subnet_ids | ||
database_availability_zone = module.vpc_subnet_setup.az_zones[0] | ||
database_security_groups = [aws_security_group.rds_sg.id] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
resource "aws_security_group" "ecs_sg" { | ||
vpc_id = module.vpc_subnet_setup.vpc_id | ||
|
||
ingress { | ||
from_port = 22 | ||
to_port = 22 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
ingress { | ||
from_port = 443 | ||
to_port = 443 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
ingress { | ||
from_port = 8000 | ||
to_port = 8000 | ||
protocol = "tcp" | ||
cidr_blocks = [ "0.0.0.0/0" ] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
tags = { | ||
Name = "${var.project_name}-ecs-sg" | ||
Owner = var.owner | ||
} | ||
} | ||
|
||
resource "aws_security_group" "rds_sg" { | ||
vpc_id = module.vpc_subnet_setup.vpc_id | ||
|
||
ingress { | ||
protocol = "tcp" | ||
from_port = var.database_port | ||
to_port = var.database_port | ||
cidr_blocks = ["0.0.0.0/0"] | ||
security_groups = [aws_security_group.ecs_sg.id] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
tags = { | ||
Name = "${var.project_name}-rds-sg" | ||
Owner = var.owner | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.