generated from side8/deploy-account.pipeline.side8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.yml
85 lines (78 loc) · 2.87 KB
/
cloudformation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
AWSTemplateFormatVersion: 2010-09-09
Description: "deploy-account.pipeline.side8"
Parameters:
Environment:
Type: String
Side8Id:
Type: String
AccountId:
Type: String
Conditions:
AccountsMatch: !Equals
- !Ref AccountId
- !Ref AWS::AccountId
Resources:
Side8PipelineIamRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub side8.pipeline.deploy.${Side8Id}
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
AWS: arn:aws:iam::265928549752:root
Action: sts:AssumeRole
Condition:
StringEquals:
sts:ExternalId: !Ref Side8Id
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AdministratorAccess'
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
AccountsMatch:
Type: Custom::AccountsMatch
Properties:
ServiceToken: !GetAtt FailFalseLambdaFunction.Arn
Boolean: !If [AccountsMatch, true, false]
FailMessage: "Account mismatch. Do you need to switch accounts?"
BasicLambdaFunctionIamRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: "sts:AssumeRole"
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
FailFalseLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Role: !GetAtt BasicLambdaFunctionIamRole.Arn
Runtime: "python3.7"
Code:
ZipFile: |
from botocore.vendored import requests
import json
def handler(event, context):
try:
boolean = json.loads(event["ResourceProperties"].get("Boolean", "false"))
except json.decoder.JSONDecodeError:
boolean = False
status = "SUCCESS" if boolean or event["RequestType"] == "Delete" else "FAILED"
message = None if status == "SUCCESS" else event["ResourceProperties"].get("FailMessage")
response = requests.put(event['ResponseURL'],
json={
"Status": status,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"PhysicalResourceId": f"""{event['LogicalResourceId']}-{status}""",
**({"Reason": message} if message is not None else {}),
})
response.raise_for_status()
return response.text