-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.yml
142 lines (129 loc) · 4.85 KB
/
macros.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Macros Template - All Accounts - Multi-region
Parameters:
SharedAccountId:
Type: String
Description: Shared AWS Account ID
AllowedPattern: "^[0-9]{12}"
Resources:
LambdaPermissionFunctionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: !Sub "lambda.${AWS::URLSuffix}"
Action: "sts:AssumeRole"
Description: !Sub "DO NOT DELETE - Used by Lambda. Created by CloudFormation ${AWS::StackId}"
Path: "/smoketurner/"
Policies:
- PolicyName: LambdaPermissionFunctionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "sts:AssumeRole"
Resource: !Sub "arn:${AWS::Partition}:iam::${SharedAccountId}:role/smoketurner/LambdaPermissionRole"
Tags:
- Key: Owner
Value: Smoke Turner
- Key: Environment
Value: PROD
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: LambdaPermissionFunctionRole
LambdaPermissionFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Properties:
LogGroupName: !Sub "/aws/lambda/${LambdaPermissionFunction}"
RetentionInDays: 3
CloudWatchLogsPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: CloudWatchLogs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !GetAtt LambdaPermissionFunctionLogGroup.Arn
Roles:
- !Ref LambdaPermissionFunctionRole
LambdaPermissionFunction:
Type: "AWS::Lambda::Function"
Properties:
Code:
ZipFile: |-
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import boto3
import botocore
import cfnresponse
ASSUME_ROLE_ARN = os.environ["ASSUME_ROLE_ARN"]
ACCOUNT_ID = os.environ["ACCOUNT_ID"]
sts = boto3.client("sts")
physical_id = "LambdaPermission"
def handler(event, context):
function_arn = event["ResourceProperties"]["FunctionArn"]
statement_id = f"Account{ACCOUNT_ID}"
status = cfnresponse.SUCCESS
try:
credentials = sts.assume_role(
RoleArn=ASSUME_ROLE_ARN, RoleSessionName="lambda-permission"
)["Credentials"]
client = boto3.client(
"lambda",
aws_access_key_id=credentials["AccessKeyId"],
aws_secret_access_key=credentials["SecretAccessKey"],
aws_session_token=credentials["SessionToken"],
)
if event["RequestType"] == "Delete":
client.remove_permission(
FunctionName=function_arn, StatementId=statement_id
)
else:
client.add_permission(
FunctionName=function_arn,
StatementId=statement_id,
Action="lambda:InvokeFunction",
Principal=ACCOUNT_ID,
)
except botocore.exceptions.ClientError as error:
print(str(error))
status = cfnresponse.FAILED
finally:
cfnresponse.send(event, context, status, {}, physical_id)
Description: DO NOT DELETE - Smoke Turner - Lambda Permission Custom Resource
Environment:
Variables:
ASSUME_ROLE_ARN: !Sub "arn:${AWS::Partition}:iam::${SharedAccountId}:role/smoketurner/LambdaPermissionRole"
ACCOUNT_ID: !Ref "AWS::AccountId"
Handler: index.handler
MemorySize: 128
Timeout: 5
Role: !GetAtt LambdaPermissionFunctionRole.Arn
Runtime: python3.8
LambdaPermission:
Type: "Custom::LambdaPermission"
DependsOn: CloudWatchLogsPolicy
Properties:
ServiceToken: !GetAtt LambdaPermissionFunction.Arn
FunctionArn: !Sub "arn:${AWS::Partition}:lambda:${AWS::Region}:${SharedAccountId}:function:LatestLayerFunction"
LatestLayerMacro:
Type: "AWS::CloudFormation::Macro"
DependsOn: LambdaPermission
Properties:
Description: DO NOT DELETE - Smoke Turner - LatestLayer Macro
FunctionName: !Sub "arn:${AWS::Partition}:lambda:${AWS::Region}:${SharedAccountId}:function:LatestLayerFunction"
Name: LatestLayer