diff --git a/env/dev/README.md b/env/dev/README.md index be50fcb..5e5c283 100644 --- a/env/dev/README.md +++ b/env/dev/README.md @@ -18,6 +18,8 @@ The optional components can be removed by simply deleting the `.tf` file. | [autoscale-perf.tf][edap] | Performance-based auto scaling | Yes | | [autoscale-time.tf][edat] | Time-based auto scaling | Yes | | [logs-logzio.tf][edll] | Ship container logs to logz.io | Yes | +| [secretsmanager.tf][edsm] | Add a Secrets Manager secret with a CMK KMS key. Also gives app role and ECS task definition role access to read secrets from Secrets Manager | Yes | +| [ssm-parameters.tf][ssm] | Add a CMK KMS key for use with SSM Parameter Store. Also gives ECS task definition role access to read secrets from parameter store. | Yes | ## Usage @@ -92,3 +94,5 @@ $ terraform apply [edll]: logs-logzio.tf [alb-docs]: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html [up]: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html +[edsm]: secretsmanager.tf +[ssm]: ssm-parameters.tf diff --git a/env/dev/ecs-event-stream.tf b/env/dev/ecs-event-stream.tf new file mode 100644 index 0000000..8fe7d35 --- /dev/null +++ b/env/dev/ecs-event-stream.tf @@ -0,0 +1,120 @@ +/** + * ECS Event Stream + * This component gives you full access to the ECS event logs + * for your cluster by creating a cloudwatch event rule that listens for + * events for this cluster and calls a lambda that writes them to cloudwatch logs. + * It then adds a cloudwatch dashboard the displays the results of a + * logs insights query against the lambda logs + */ + +# cw event rule +resource "aws_cloudwatch_event_rule" "ecs_event_stream" { + name = "${var.app}-${var.environment}-ecs-event-stream" + description = "Passes ecs event logs for ${var.app}-${var.environment} to a lambda that writes them to cw logs" + + event_pattern = < { + console.log(JSON.stringify(event)); +} +EOF + +} + +data "archive_file" "lambda_zip" { + type = "zip" + source_content = data.template_file.lambda_source.rendered + source_content_filename = "index.js" + output_path = "lambda-${var.app}.zip" +} + +resource "aws_lambda_permission" "ecs_event_stream" { + statement_id = "AllowExecutionFromCloudWatch" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.ecs_event_stream.arn + principal = "events.amazonaws.com" + source_arn = aws_cloudwatch_event_rule.ecs_event_stream.arn +} + +resource "aws_lambda_function" "ecs_event_stream" { + function_name = "${var.app}-${var.environment}-ecs-event-stream" + role = aws_iam_role.ecs_event_stream.arn + filename = data.archive_file.lambda_zip.output_path + source_code_hash = data.archive_file.lambda_zip.output_base64sha256 + handler = "index.handler" + runtime = "nodejs8.10" + tags = var.tags +} + +resource "aws_lambda_alias" "ecs_event_stream" { + name = aws_lambda_function.ecs_event_stream.function_name + description = "latest" + function_name = aws_lambda_function.ecs_event_stream.function_name + function_version = "$LATEST" +} + +resource "aws_iam_role" "ecs_event_stream" { + name = aws_cloudwatch_event_rule.ecs_event_stream.name + + assume_role_policy = <