1+ resource "aws_cloudwatch_metric_stream" "main" {
2+ name = " my-metric-stream"
3+ role_arn = aws_iam_role. metric_stream_to_firehose . arn
4+ firehose_arn = aws_kinesis_firehose_delivery_stream. s3_stream . arn
5+ output_format = " json"
6+
7+ include_filter {
8+ namespace = " AWS/EC2"
9+ metric_names = [" CPUUtilization" , " NetworkOut" ]
10+ }
11+
12+ include_filter {
13+ namespace = " AWS/EBS"
14+ metric_names = []
15+ }
16+ }
17+
18+ # https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-metric-streams-trustpolicy.html
19+ data "aws_iam_policy_document" "streams_assume_role" {
20+ statement {
21+ effect = " Allow"
22+
23+ principals {
24+ type = " Service"
25+ identifiers = [" streams.metrics.cloudwatch.amazonaws.com" ]
26+ }
27+
28+ actions = [
29+ " sts:AssumeRole" ,
30+ " iam:passRole" ,
31+ " cloudwatch:PutMetricStream"
32+ ]
33+ }
34+ }
35+
36+ resource "aws_iam_role" "metric_stream_to_firehose" {
37+ name = " metric_stream_to_firehose_role"
38+ assume_role_policy = data. aws_iam_policy_document . streams_assume_role . json
39+ }
40+
41+ # https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-metric-streams-trustpolicy.html
42+ data "aws_iam_policy_document" "metric_stream_to_firehose" {
43+ statement {
44+ effect = " Allow"
45+
46+ actions = [
47+ " firehose:PutRecord" ,
48+ " firehose:PutRecordBatch" ,
49+ ]
50+
51+ resources = [aws_kinesis_firehose_delivery_stream . s3_stream . arn ]
52+ }
53+ }
54+ resource "aws_iam_role_policy" "metric_stream_to_firehose" {
55+ name = " default"
56+ role = aws_iam_role. metric_stream_to_firehose . id
57+ policy = data. aws_iam_policy_document . metric_stream_to_firehose . json
58+ }
59+
60+ resource "aws_s3_bucket" "bucket" {
61+ bucket = " metric-stream-test-bucket"
62+ }
63+
64+ resource "aws_s3_bucket_acl" "bucket_acl" {
65+ bucket = aws_s3_bucket. bucket . id
66+ acl = " private"
67+ }
68+
69+ data "aws_iam_policy_document" "firehose_assume_role" {
70+ statement {
71+ effect = " Allow"
72+
73+ principals {
74+ type = " Service"
75+ identifiers = [" firehose.amazonaws.com" ]
76+ }
77+
78+ actions = [
79+ " sts:AssumeRole" ,
80+ " iam:passRole" ,
81+ " cloudwatch:PutMetricStream"
82+ ]
83+ }
84+ }
85+
86+ resource "aws_iam_role" "firehose_to_s3" {
87+ assume_role_policy = data. aws_iam_policy_document . firehose_assume_role . json
88+ }
89+
90+ data "aws_iam_policy_document" "firehose_to_s3" {
91+ statement {
92+ effect = " Allow"
93+
94+ actions = [
95+ " s3:AbortMultipartUpload" ,
96+ " s3:GetBucketLocation" ,
97+ " s3:GetObject" ,
98+ " s3:ListBucket" ,
99+ " s3:ListBucketMultipartUploads" ,
100+ " s3:PutObject" ,
101+ ]
102+
103+ resources = [
104+ aws_s3_bucket . bucket . arn ,
105+ " ${ aws_s3_bucket . bucket . arn } /*" ,
106+ ]
107+ }
108+ }
109+
110+ resource "aws_iam_role_policy" "firehose_to_s3" {
111+ name = " default"
112+ role = aws_iam_role. firehose_to_s3 . id
113+ policy = data. aws_iam_policy_document . firehose_to_s3 . json
114+ }
115+
116+ resource "aws_kinesis_firehose_delivery_stream" "s3_stream" {
117+ name = " metric-stream-test-stream"
118+ destination = " s3"
119+
120+ s3_configuration {
121+ role_arn = aws_iam_role. firehose_to_s3 . arn
122+ bucket_arn = aws_s3_bucket. bucket . arn
123+ }
124+ }
125+
126+ resource "aws_iam_user" "ecs_deployer" {
127+ name = " ecs_deployer"
128+ path = " *"
129+ }
130+
131+ # The most important part is the iam:PassRole. With that, this user can give roles to ECS tasks.
132+ # In theory the user can give the task Admin rights. To make sure that does not happen we restrict
133+ # the user and allow him only to hand out roles in /ecs/ path. You still need to be careful not
134+ # to have any roles in there with full admin rights, but no ECS task should have these rights!
135+ resource "aws_iam_user_policy" "ecs_deployer_policy" {
136+ name = " ecs_deployer_policy"
137+ user = aws_iam_user. ecs_deployer . name
138+ policy = jsonencode (
139+ {
140+ " Version" : " 2012-10-17" ,
141+ " Statement" : [
142+ {
143+ " Effect" : " Allow" ,
144+ " Action" : [
145+ " ecs:RegisterTaskDefinition" ,
146+ " ecs:DescribeTaskDefinitions" ,
147+ " ecs:ListTaskDefinitions" ,
148+ " ecs:CreateService" ,
149+ " ecs:UpdateService" ,
150+ " ecs:DescribeServices" ,
151+ " ecs:ListServices"
152+ ],
153+ " Resource" : " *"
154+ },
155+ {
156+ " Effect" : " Allow" ,
157+ " Action" : [
158+ " cloudwatch:PutMetricStream"
159+ ],
160+ " Resource" : " *"
161+ },
162+ {
163+ " Effect" : " Allow" ,
164+ " Action" : [" iam:PassRole" ],
165+ " Resource" : " *"
166+ }
167+ ]
168+ })
169+ }
170+
171+ resource "aws_iam_access_key" "ecs_deployer" {
172+ user = aws_iam_user. ecs_deployer . name
173+ }
0 commit comments