Skip to content

Commit 89c2aa8

Browse files
twarnockMark Beacom
authored and
Mark Beacom
committed
Step sqs (#84)
* sqs checkin * state now printing to logs from each step
1 parent cea7fd2 commit 89c2aa8

17 files changed

+343
-106
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,6 @@ venv.bak/
114114
.terraform/
115115
terraform.tfstate*
116116
*.tfvars
117+
118+
# vscode
119+
.vscode/

step/cloudwatch.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// retention_in_days = "1"
44
// }
55

6-
resource "aws_cloudwatch_event_rule" "rehost-migration-rule" {
6+
resource "aws_cloudwatch_event_rule" "rehost_migration_rule" {
77
name = "ce-rehost-migration-rule"
88
description = ""
99
event_pattern = <<PATTERN
@@ -23,8 +23,14 @@ resource "aws_cloudwatch_event_rule" "rehost-migration-rule" {
2323
PATTERN
2424
}
2525

26-
resource "aws_cloudwatch_event_target" "rehost-migration-target" {
27-
rule = "${aws_cloudwatch_event_rule.rehost-migration-rule.id}"
26+
resource "aws_cloudwatch_event_target" "rehost_migration_target" {
27+
rule = "${aws_cloudwatch_event_rule.rehost_migration_rule.id}"
2828
arn = "${aws_sfn_state_machine.rehost_migration.id}"
29-
role_arn = "${aws_iam_role.iam_for_stepfunction.arn}"
29+
role_arn = "${aws_iam_role.iam_for_cloudwatch_stepfunction.arn}"
30+
}
31+
32+
resource "aws_lambda_event_source_mapping" "event_source_mapping" {
33+
event_source_arn = "${aws_sqs_queue.event_queue.arn}"
34+
function_name = "${aws_lambda_function.lambda_update_servicenow.arn}"
35+
batch_size = 1
3036
}

step/iam.tf

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
# step function related iam
1+
# sfn related iam role
22
resource "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
78
data "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
4644
resource "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
5150
data "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+
6670
resource "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+
}

step/lambdas.tf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ resource "aws_lambda_function" "lambda_find_instance" {
1313
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
1414
runtime = "python3.7"
1515
depends_on = ["data.archive_file.lambdas"]
16+
17+
environment {
18+
variables = {
19+
event_queue = "${aws_sqs_queue.event_queue.id}"
20+
}
21+
}
1622
}
1723

1824
resource "aws_lambda_function" "lambda_get_instance_status" {
@@ -23,6 +29,12 @@ resource "aws_lambda_function" "lambda_get_instance_status" {
2329
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
2430
runtime = "python3.7"
2531
depends_on = ["data.archive_file.lambdas"]
32+
33+
environment {
34+
variables = {
35+
event_queue = "${aws_sqs_queue.event_queue.id}"
36+
}
37+
}
2638
}
2739

2840
resource "aws_lambda_function" "lambda_create_image" {
@@ -33,6 +45,12 @@ resource "aws_lambda_function" "lambda_create_image" {
3345
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
3446
runtime = "python3.7"
3547
depends_on = ["data.archive_file.lambdas"]
48+
49+
environment {
50+
variables = {
51+
event_queue = "${aws_sqs_queue.event_queue.id}"
52+
}
53+
}
3654
}
3755

3856
resource "aws_lambda_function" "lambda_get_image_status" {
@@ -43,6 +61,12 @@ resource "aws_lambda_function" "lambda_get_image_status" {
4361
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
4462
runtime = "python3.7"
4563
depends_on = ["data.archive_file.lambdas"]
64+
65+
environment {
66+
variables = {
67+
event_queue = "${aws_sqs_queue.event_queue.id}"
68+
}
69+
}
4670
}
4771

4872
resource "aws_lambda_function" "lambda_share_image" {
@@ -53,6 +77,12 @@ resource "aws_lambda_function" "lambda_share_image" {
5377
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
5478
runtime = "python3.7"
5579
depends_on = ["data.archive_file.lambdas"]
80+
81+
environment {
82+
variables = {
83+
event_queue = "${aws_sqs_queue.event_queue.id}"
84+
}
85+
}
5686
}
5787

5888
resource "aws_lambda_function" "lambda_copy_image" {
@@ -63,6 +93,12 @@ resource "aws_lambda_function" "lambda_copy_image" {
6393
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
6494
runtime = "python3.7"
6595
depends_on = ["data.archive_file.lambdas"]
96+
97+
environment {
98+
variables = {
99+
event_queue = "${aws_sqs_queue.event_queue.id}"
100+
}
101+
}
66102
}
67103

68104
resource "aws_lambda_function" "lambda_get_copy_status" {
@@ -73,6 +109,12 @@ resource "aws_lambda_function" "lambda_get_copy_status" {
73109
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
74110
runtime = "python3.7"
75111
depends_on = ["data.archive_file.lambdas"]
112+
113+
environment {
114+
variables = {
115+
event_queue = "${aws_sqs_queue.event_queue.id}"
116+
}
117+
}
76118
}
77119

78120
resource "aws_lambda_function" "lambda_split_image" {
@@ -83,6 +125,12 @@ resource "aws_lambda_function" "lambda_split_image" {
83125
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
84126
runtime = "python3.7"
85127
depends_on = ["data.archive_file.lambdas"]
128+
129+
environment {
130+
variables = {
131+
event_queue = "${aws_sqs_queue.event_queue.id}"
132+
}
133+
}
86134
}
87135

88136
resource "aws_lambda_function" "lambda_image_cleanup" {
@@ -93,4 +141,20 @@ resource "aws_lambda_function" "lambda_image_cleanup" {
93141
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
94142
runtime = "python3.7"
95143
depends_on = ["data.archive_file.lambdas"]
144+
145+
environment {
146+
variables = {
147+
event_queue = "${aws_sqs_queue.event_queue.id}"
148+
}
149+
}
150+
}
151+
152+
resource "aws_lambda_function" "lambda_update_servicenow" {
153+
filename = "lambdas.zip"
154+
function_name = "ce-update-servicenow"
155+
role = "${aws_iam_role.iam_for_lambda.arn}"
156+
handler = "update_servicenow.lambda_handler"
157+
source_code_hash = "${data.archive_file.lambdas.output_base64sha256}"
158+
runtime = "python3.7"
159+
depends_on = ["data.archive_file.lambdas"]
96160
}

step/lambdas/copy_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, Dict
99

1010
import boto3
11+
from servicenowstate import ServiceNowState, ServiceNowStateHandler
1112

1213
print("Loading function copy_image")
1314

@@ -27,6 +28,7 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
2728
kms_id: str = event["kms_id"]
2829
region: str = event.get("region", os.environ.get("AWS_REGION"))
2930
role: str = event.get("role")
31+
instance_name: str = event.get("name", "")
3032

3133
sts_client = boto3.client("sts")
3234

@@ -57,4 +59,7 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
5759
print(e)
5860
return ""
5961

62+
ServiceNowStateHandler().update_state(
63+
state="IMAGE_COPYING", machine_name=instance_name
64+
)
6065
return new_image.get("ImageId", "")

step/lambdas/create_image.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, Dict
99

1010
import boto3
11+
from servicenowstate import ServiceNowStateHandler
1112

1213
print("Loading function create_image")
1314

@@ -26,8 +27,8 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
2627
print("Received event: " + json.dumps(event, indent=2))
2728

2829
instance_id: str = event["instance_id"]
29-
3030
image_creation_time: str = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
31+
name: str = event.get("name", "")
3132

3233
instance = ec2_resource.Instance(instance_id)
3334

@@ -41,4 +42,6 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
4142

4243
instance.create_tags(Tags=[{"Key": "CloneStatus", "Value": "IMAGE_CREATED"}])
4344

45+
ServiceNowStateHandler().update_state(state="IMAGE_CREATING", machine_name=name)
46+
4447
return ec2_image.image_id

step/lambdas/find_instance.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from __future__ import annotations
55

66
import json
7+
import os
78
from typing import Any, Dict
89

910
import boto3
11+
from servicenowstate import ServiceNowStateHandler
1012

1113
print("Loading function find_instance")
1214

1315
ec2_resource = boto3.resource("ec2")
16+
sqs = boto3.client("sqs")
1417

1518
# {
1619
# "version": "0",
@@ -62,8 +65,14 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
6265
if tag["Key"] == "DestinationRole":
6366
event_dict["role"] = tag["Value"]
6467

68+
if tag["Key"] == "Name":
69+
event_dict["name"] = tag["Value"]
70+
6571
except Exception as e:
6672
print(e)
6773
event_dict["instance_id"] = "not-found"
6874

75+
ServiceNowStateHandler().update_state(
76+
state="INSTANCE_LAUNCHED", machine_name=event_dict.get("name")
77+
)
6978
return event_dict

0 commit comments

Comments
 (0)