generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Sends logs using rsyslog fropm the ECS instances, to the given endpoint. * Launches a Logspout container on each ECS instance, to send Docker logs to the given endpoint. * This allows logs to be collected externally, eg. Papertrail/Logstash
- Loading branch information
Showing
13 changed files
with
311 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
version: 0.2 | ||
|
||
phases: | ||
pre_build: | ||
commands: | ||
- echo "Build started on $(date)" | ||
- echo "Logging in to Amazon ECR..." | ||
- aws ecr get-login-password --region "$AWS_DEFAULT_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com" | ||
- | | ||
if [ -n "$DOCKERHUB_USERNAME" ] && [ -n "DOCKERHUB_TOKEN" ]; | ||
then | ||
echo "Logging into Dockerhub ..."; | ||
echo "$DOCKERHUB_TOKEN" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin; | ||
fi; | ||
- echo "Pulling Logspout docker image ..." | ||
- docker pull gliderlabs/logspout:v3.2.14 | ||
build: | ||
commands: | ||
- echo "Adding ECR repo tag..." | ||
- docker tag gliderlabs/logspout:v3.2.14 "$REPOSITORY_URI:latest" | ||
post_build: | ||
commands: | ||
- echo "Pushing the Docker image..." | ||
- docker push "$REPOSITORY_URI:latest" |
This file contains 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 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 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,17 @@ | ||
resource "aws_ecr_repository" "infrastructure_ecs_cluster_logspout" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-logspout" | ||
|
||
#tfsec:ignore:aws-ecr-enforce-immutable-repository | ||
image_tag_mutability = "MUTABLE" | ||
|
||
encryption_configuration { | ||
encryption_type = local.infrastructure_kms_encryption ? "KMS" : "AES256" | ||
kms_key = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
} | ||
|
||
image_scanning_configuration { | ||
scan_on_push = true | ||
} | ||
} |
This file contains 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,146 @@ | ||
resource "aws_iam_role" "infrastructure_ecs_cluster_logspout_image_codebuild" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-logspout-image-codebuild"), 0, 6)}" | ||
description = "${local.resource_prefix}-ecs-cluster-logspout-image-codebuild" | ||
assume_role_policy = templatefile( | ||
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl", | ||
{ services = jsonencode(["codebuild.amazonaws.com", "events.amazonaws.com"]) } | ||
) | ||
} | ||
|
||
resource "aws_iam_policy" "infrastructure_ecs_cluster_logspout_image_codebuild_cloudwatch_rw" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-logspout-image-codebuild-cloudwatch-rw"), 0, 6)}" | ||
description = "${local.resource_prefix}-ecs-cluster-logspout-image-codebuild-cloudwatch-rw" | ||
policy = templatefile("${path.root}/policies/cloudwatch-logs-rw.json.tpl", {}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_logspout_image_codebuild_cloudwatch_rw" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
role = aws_iam_role.infrastructure_ecs_cluster_logspout_image_codebuild[0].name | ||
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_logspout_image_codebuild_cloudwatch_rw[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "infrastructure_ecs_cluster_logspout_image_codebuild_allow_builds" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-logspout-image-codebuild-allow-builds"), 0, 6)}" | ||
description = "${local.resource_prefix}-ecs-cluster-logspout-image-codebuild-allow-builds" | ||
policy = templatefile("${path.root}/policies/codebuild-allow-builds.json.tpl", {}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_logspout_image_codebuild_allow_builds" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
role = aws_iam_role.infrastructure_ecs_cluster_logspout_image_codebuild[0].name | ||
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_logspout_image_codebuild_allow_builds[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "infrastructure_ecs_cluster_logspout_image_codebuild_ecr_push" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-${substr(sha512("ecs-cluster-logspout-image-codebuild-ecr-push"), 0, 6)}" | ||
description = "${local.resource_prefix}-ecs-cluster-logspout-image-codebuild-ecr-push" | ||
policy = templatefile( | ||
"${path.root}/policies/ecr-push.json.tpl", | ||
{ ecr_repository_arn = aws_ecr_repository.infrastructure_ecs_cluster_logspout[0].arn } | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_logspout_image_codebuild_ecr_push" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
role = aws_iam_role.infrastructure_ecs_cluster_logspout_image_codebuild[0].name | ||
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_logspout_image_codebuild_ecr_push[0].arn | ||
} | ||
|
||
resource "aws_codebuild_project" "infrastructure_ecs_cluster_logspout_image_build" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-cluster-logspout-image-build" | ||
description = "${local.resource_prefix} ECS Cluster Logspout Image Build" | ||
build_timeout = "20" | ||
service_role = aws_iam_role.infrastructure_ecs_cluster_logspout_image_codebuild[0].arn | ||
|
||
artifacts { | ||
type = "NO_ARTIFACTS" | ||
} | ||
|
||
environment { | ||
compute_type = "BUILD_GENERAL1_SMALL" | ||
image = "aws/codebuild/standard:7.0" | ||
type = "LINUX_CONTAINER" | ||
privileged_mode = true | ||
|
||
environment_variable { | ||
name = "AWS_ACCOUNT_ID" | ||
value = local.aws_account_id | ||
} | ||
|
||
environment_variable { | ||
name = "REPOSITORY_URI" | ||
value = aws_ecr_repository.infrastructure_ecs_cluster_logspout[0].repository_url | ||
} | ||
|
||
environment_variable { | ||
name = "DOCKERHUB_USERNAME" | ||
value = local.infrastructure_dockerhub_username | ||
} | ||
|
||
environment_variable { | ||
name = "DOCKERHUB_TOKEN" | ||
value = local.infrastructure_dockerhub_token | ||
} | ||
} | ||
|
||
source { | ||
type = "NO_SOURCE" | ||
buildspec = templatefile("${path.root}/buildspecs/dalmatian-logspout.yml", {}) | ||
} | ||
|
||
depends_on = [ | ||
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_logspout_image_codebuild_cloudwatch_rw, | ||
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_logspout_image_codebuild_allow_builds, | ||
aws_iam_role_policy_attachment.infrastructure_ecs_cluster_logspout_image_codebuild_ecr_push, | ||
] | ||
} | ||
|
||
resource "terraform_data" "infrastructure_ecs_cluster_logspout_image_build_trigger_codebuild" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
triggers_replace = [ | ||
md5(templatefile("${path.root}/buildspecs/dalmatian-logspout.yml", {})), | ||
] | ||
|
||
provisioner "local-exec" { | ||
interpreter = ["/bin/bash", "-c"] | ||
command = <<EOF | ||
${path.root}/local-exec-scripts/trigger-codedeploy-project.sh \ | ||
-n "${aws_codebuild_project.infrastructure_ecs_cluster_logspout_image_build[0].name}" | ||
EOF | ||
} | ||
|
||
depends_on = [ | ||
|
||
] | ||
} | ||
|
||
resource "aws_cloudwatch_event_rule" "infrastructure_ecs_cluster_logspout_image_build_trigger_codebuild" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix_hash}-ecs-cluster-logspout-image-build-trigger-codebuild" | ||
description = "${local.resource_prefix} ECS Cluster Logspout Image Build Trigger CodeBuild" | ||
schedule_expression = "rate(24 hours)" | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "infrastructure_ecs_cluster_logspout_image_build_trigger_codebuild" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
target_id = "${local.resource_prefix_hash}-ecs-cluster-image-build-trigger-codebuild" | ||
rule = aws_cloudwatch_event_rule.infrastructure_ecs_cluster_logspout_image_build_trigger_codebuild[0].name | ||
arn = aws_codebuild_project.infrastructure_ecs_cluster_logspout_image_build[0].id | ||
role_arn = aws_iam_role.infrastructure_ecs_cluster_logspout_image_codebuild[0].arn | ||
} |
This file contains 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,45 @@ | ||
resource "aws_ecs_task_definition" "infrastructure_ecs_cluster_logspout" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
family = "${local.resource_prefix}-ecs-infrastructure-logsput" | ||
container_definitions = templatefile( | ||
"./container-definitions/app.json.tpl", | ||
{ | ||
container_name = "logspout" | ||
image = aws_ecr_repository.infrastructure_ecs_cluster_logspout[0].repository_url | ||
entrypoint = jsonencode([]) | ||
command = jsonencode([local.infrastructure_ecs_cluster_syslog_endpoint]) | ||
environment_file_s3 = "" | ||
environment = jsonencode([]) | ||
secrets = jsonencode([]) | ||
container_port = 0 | ||
extra_hosts = jsonencode([]) | ||
volumes = jsonencode([ | ||
{ | ||
sourceVolume = "dockersock" | ||
containerPath = "/var/run/docker.sock" | ||
} | ||
]) | ||
linux_parameters = jsonencode({}) | ||
cloudwatch_log_group = "" | ||
awslogs_stream_prefix = "" | ||
region = local.aws_region | ||
} | ||
) | ||
|
||
volume { | ||
name = "dockersock" | ||
host_path = "/var/run/docker.sock" | ||
} | ||
network_mode = "bridge" | ||
requires_compatibilities = ["EC2"] | ||
} | ||
|
||
resource "aws_ecs_service" "infrastructure_ecs_cluster_logspout" { | ||
count = local.infrastrucutre_ecs_cluster_logspout_enabled ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-ecs-infrastructure-logsput" | ||
cluster = aws_ecs_cluster.infrastructure[0].name | ||
task_definition = aws_ecs_task_definition.infrastructure_ecs_cluster_logspout[0].arn | ||
scheduling_strategy = "DAEMON" | ||
} |
This file contains 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 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 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 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 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 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