1- # step function related iam
1+ # sfn related iam role
22resource "aws_iam_role" "iam_for_stepfunction" {
33 name = " ce-iam-for-stepfunction"
44 assume_role_policy = " ${ data . aws_iam_policy_document . stepfunction_assume_role_policy_document . json } "
55}
66
7+ # assume_role_policy for sfn role
78data "aws_iam_policy_document" "stepfunction_assume_role_policy_document" {
89 statement {
910 actions = [" sts:AssumeRole" ]
1011
1112 principals {
1213 type = " Service"
13- identifiers = [" states.${ var . region } .amazonaws.com" ]
14+ identifiers = [
15+ " states.${ var . region } .amazonaws.com"
16+ ]
1417 }
1518 }
1619}
1720
18- data "aws_iam_policy_document" "lambda-invoke" {
21+ # sfn policy needed to invoke lambda
22+ resource "aws_iam_policy" "lambda_invoke" {
23+ name = " ce-lambda-invoke"
24+ policy = " ${ data . aws_iam_policy_document . lambda_invoke . json } "
25+ }
26+
27+ data "aws_iam_policy_document" "lambda_invoke" {
1928 statement {
2029 actions = [
2130 " lambda:InvokeFunction"
@@ -24,30 +33,20 @@ data "aws_iam_policy_document" "lambda-invoke" {
2433 " *" ,
2534 ]
2635 }
27- # role(s) that the lambdas are allowed to assume roles on for copy, split, and tf generation
28- statement {
29- effect = " Allow"
30- actions = [ " sts:AssumeRole" ]
31- resources = [for role in var . assume_role_list : role ]
32- }
3336}
3437
35- resource "aws_iam_policy" "lambda-invoke" {
36- name = " ce-lambda-invoke"
37- policy = " ${ data . aws_iam_policy_document . lambda-invoke . json } "
38- }
39-
40- resource "aws_iam_role_policy_attachment" "lambda-invoke" {
38+ resource "aws_iam_role_policy_attachment" "lambda_invoke" {
4139 role = " ${ aws_iam_role . iam_for_stepfunction . name } "
42- policy_arn = " ${ aws_iam_policy . lambda-invoke . arn } "
40+ policy_arn = " ${ aws_iam_policy . lambda_invoke . arn } "
4341}
4442
45- # lambda related
43+ # lambda related iam role
4644resource "aws_iam_role" "iam_for_lambda" {
4745 name = " ce-iam-for-lambda"
4846 assume_role_policy = " ${ data . aws_iam_policy_document . iam_for_lambda_assume_role . json } "
4947}
5048
49+ # assume_role_policy for lambda role
5150data "aws_iam_policy_document" "iam_for_lambda_assume_role" {
5251 statement {
5352 actions = [" sts:AssumeRole" ]
@@ -63,8 +62,78 @@ resource "aws_iam_role_policy_attachment" "role_policy_lambda_exec" {
6362 policy_arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
6463}
6564
65+ resource "aws_iam_role_policy_attachment" "role_policy_lambda_sqs" {
66+ role = " ${ aws_iam_role . iam_for_lambda . name } "
67+ policy_arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole"
68+ }
69+
6670resource "aws_iam_role_policy_attachment" "role_policy_lambda_ec2" {
6771 role = " ${ aws_iam_role . iam_for_lambda . name } "
6872 policy_arn = " arn:aws:iam::aws:policy/AmazonEC2FullAccess"
6973}
70-
74+
75+ # create policy to allow SQS and AssumeRole
76+ resource "aws_iam_policy" "role_policy_lambda_execution" {
77+ name = " ce-lambda-execution-policy"
78+ policy = " ${ data . aws_iam_policy_document . role_policy_lambda_execution_document . json } "
79+ }
80+
81+ data "aws_iam_policy_document" "role_policy_lambda_execution_document" {
82+ statement {
83+ effect = " Allow"
84+ actions = [
85+ " sqs:SendMessage" ,
86+ " sqs:GetQueueUrl"
87+ ]
88+ resources = [
89+ " ${ aws_sqs_queue . event_queue . arn } "
90+ ]
91+ }
92+
93+ # role(s) that the lambdas are allowed to assume roles on for copy, split, and tf generation
94+ statement {
95+ effect = " Allow"
96+ actions = [ " sts:AssumeRole" ]
97+ resources = [for role in var . assume_role_list : role ]
98+ }
99+ }
100+
101+ resource "aws_iam_role_policy_attachment" "role_policy_lambda_execution" {
102+ role = " ${ aws_iam_role . iam_for_lambda . name } "
103+ policy_arn = " ${ aws_iam_policy . role_policy_lambda_execution . arn } "
104+ }
105+
106+ // CW event execution
107+
108+ resource "aws_iam_role" "iam_for_cloudwatch_stepfunction" {
109+ name = " ce-cloudwatch-stepfunction"
110+ assume_role_policy = " ${ data . aws_iam_policy_document . stepfunction_assume_role_document . json } "
111+ }
112+
113+ data "aws_iam_policy_document" "stepfunction_assume_role_document" {
114+ statement {
115+ actions = [" sts:AssumeRole" ]
116+ principals {
117+ type = " Service"
118+ identifiers = [" events.amazonaws.com" ]
119+ }
120+ }
121+ }
122+
123+ resource "aws_iam_policy" "stepfunction_execution" {
124+ name = " ce-cloudwatch-stepfunction"
125+ policy = " ${ data . aws_iam_policy_document . stepfunction_execution_policy_document . json } "
126+ }
127+
128+ data "aws_iam_policy_document" "stepfunction_execution_policy_document" {
129+ statement {
130+ effect = " Allow"
131+ actions = [" states:StartExecution" ]
132+ resources = [" ${ aws_sfn_state_machine . rehost_migration . id } " ]
133+ }
134+ }
135+
136+ resource "aws_iam_role_policy_attachment" "stepfunction_execution_attachment" {
137+ role = " ${ aws_iam_role . iam_for_cloudwatch_stepfunction . name } "
138+ policy_arn = " ${ aws_iam_policy . stepfunction_execution . arn } "
139+ }
0 commit comments