Skip to content

Commit c3c771c

Browse files
authored
feat(ses): grant methods to IEmailIdentity (#29084)
### Issue Closes #29083 ### Reason for this change When granting send email access to a lambda the grant needs to be constructed manually, including constructing the ARN for the identity. e.g. ``` Grant.addToPrincipal({ grantee, actions: ["ses:SendEmail"], resourceArns: [ this.stack.formatArn({ service: 'ses', resource: 'identity', resourceName: '[email protected]', }), ], scope: this }) ``` This is dissimilar to other constructs, which generally expose a grant method and one or more convenience methods for particularly relevant groups of actions. ### Description of changes Added `grant` and `grantSendEmail` to `IEmailIdentity`, and added a common abstract class, `BaseEmailIdentity` with the relevant grant code. This is to avoid code duplication between the full `EmailIdentity` and the `Import` class. ### Description of how you validated changes Tests added for grants on both new and imported email identities, and a test to validate the `grantSendEmail` method. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e890e89 commit c3c771c

File tree

13 files changed

+749
-88
lines changed

13 files changed

+749
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import json
2+
import boto3
3+
4+
client = boto3.client('ses', region_name='us-west-2')
5+
6+
def lambda_handler(event, context):
7+
response = client.send_email(
8+
Destination={
9+
'ToAddresses': ['[email protected]']
10+
},
11+
Message={
12+
'Body': {
13+
'Text': {
14+
'Charset': 'UTF-8',
15+
'Data': 'This is the message body in text format.',
16+
}
17+
},
18+
'Subject': {
19+
'Charset': 'UTF-8',
20+
'Data': 'Test email',
21+
},
22+
},
23+
24+
)
25+
26+
print(response)
27+
28+
return {
29+
'statusCode': 200,
30+
'body': json.dumps("Email Sent Successfully. MessageId is: " + response['MessageId'])
31+
}

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/EmailIdentityIntegDefaultTestDeployAssert3F909307.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/asset.ab3156800c2f322f16da8bff913172139189a387dd64a4e622f82a790561fd4d/index.py

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/cdk-ses-email-identity-integ.assets.json

+16-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/cdk-ses-email-identity-integ.template.json

+122-21
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,119 @@
66
"Name": "cdk.dev."
77
}
88
},
9+
"FunctionServiceRole675BB04A": {
10+
"Type": "AWS::IAM::Role",
11+
"Properties": {
12+
"AssumeRolePolicyDocument": {
13+
"Statement": [
14+
{
15+
"Action": "sts:AssumeRole",
16+
"Effect": "Allow",
17+
"Principal": {
18+
"Service": "lambda.amazonaws.com"
19+
}
20+
}
21+
],
22+
"Version": "2012-10-17"
23+
},
24+
"ManagedPolicyArns": [
25+
{
26+
"Fn::Join": [
27+
"",
28+
[
29+
"arn:",
30+
{
31+
"Ref": "AWS::Partition"
32+
},
33+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
34+
]
35+
]
36+
}
37+
]
38+
}
39+
},
40+
"FunctionServiceRoleDefaultPolicy2F49994A": {
41+
"Type": "AWS::IAM::Policy",
42+
"Properties": {
43+
"PolicyDocument": {
44+
"Statement": [
45+
{
46+
"Action": [
47+
"ses:SendEmail",
48+
"ses:SendRawEmail"
49+
],
50+
"Effect": "Allow",
51+
"Resource": {
52+
"Fn::Join": [
53+
"",
54+
[
55+
"arn:",
56+
{
57+
"Ref": "AWS::Partition"
58+
},
59+
":ses:",
60+
{
61+
"Ref": "AWS::Region"
62+
},
63+
":",
64+
{
65+
"Ref": "AWS::AccountId"
66+
},
67+
":identity/",
68+
{
69+
"Ref": "EmailIdentity7187767D"
70+
}
71+
]
72+
]
73+
}
74+
}
75+
],
76+
"Version": "2012-10-17"
77+
},
78+
"PolicyName": "FunctionServiceRoleDefaultPolicy2F49994A",
79+
"Roles": [
80+
{
81+
"Ref": "FunctionServiceRole675BB04A"
82+
}
83+
]
84+
}
85+
},
86+
"Function76856677": {
87+
"Type": "AWS::Lambda::Function",
88+
"Properties": {
89+
"Code": {
90+
"S3Bucket": {
91+
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
92+
},
93+
"S3Key": "ab3156800c2f322f16da8bff913172139189a387dd64a4e622f82a790561fd4d.zip"
94+
},
95+
"FunctionName": "email-sending-lambda",
96+
"Handler": "index.lambda_handler",
97+
"Role": {
98+
"Fn::GetAtt": [
99+
"FunctionServiceRole675BB04A",
100+
"Arn"
101+
]
102+
},
103+
"Runtime": "python3.11"
104+
},
105+
"DependsOn": [
106+
"FunctionServiceRoleDefaultPolicy2F49994A",
107+
"FunctionServiceRole675BB04A"
108+
]
109+
},
9110
"EmailIdentityDkimDnsToken1BA32ACB3": {
10111
"Type": "AWS::Route53::RecordSet",
11112
"Properties": {
113+
"HostedZoneId": {
114+
"Ref": "HostedZoneDB99F866"
115+
},
12116
"Name": {
13117
"Fn::GetAtt": [
14118
"EmailIdentity7187767D",
15119
"DkimDNSTokenName1"
16120
]
17121
},
18-
"Type": "CNAME",
19-
"HostedZoneId": {
20-
"Ref": "HostedZoneDB99F866"
21-
},
22122
"ResourceRecords": [
23123
{
24124
"Fn::GetAtt": [
@@ -27,22 +127,22 @@
27127
]
28128
}
29129
],
30-
"TTL": "1800"
130+
"TTL": "1800",
131+
"Type": "CNAME"
31132
}
32133
},
33134
"EmailIdentityDkimDnsToken2BBEBB8EC": {
34135
"Type": "AWS::Route53::RecordSet",
35136
"Properties": {
137+
"HostedZoneId": {
138+
"Ref": "HostedZoneDB99F866"
139+
},
36140
"Name": {
37141
"Fn::GetAtt": [
38142
"EmailIdentity7187767D",
39143
"DkimDNSTokenName2"
40144
]
41145
},
42-
"Type": "CNAME",
43-
"HostedZoneId": {
44-
"Ref": "HostedZoneDB99F866"
45-
},
46146
"ResourceRecords": [
47147
{
48148
"Fn::GetAtt": [
@@ -51,22 +151,22 @@
51151
]
52152
}
53153
],
54-
"TTL": "1800"
154+
"TTL": "1800",
155+
"Type": "CNAME"
55156
}
56157
},
57158
"EmailIdentityDkimDnsToken3BB5E8A49": {
58159
"Type": "AWS::Route53::RecordSet",
59160
"Properties": {
161+
"HostedZoneId": {
162+
"Ref": "HostedZoneDB99F866"
163+
},
60164
"Name": {
61165
"Fn::GetAtt": [
62166
"EmailIdentity7187767D",
63167
"DkimDNSTokenName3"
64168
]
65169
},
66-
"Type": "CNAME",
67-
"HostedZoneId": {
68-
"Ref": "HostedZoneDB99F866"
69-
},
70170
"ResourceRecords": [
71171
{
72172
"Fn::GetAtt": [
@@ -75,7 +175,8 @@
75175
]
76176
}
77177
],
78-
"TTL": "1800"
178+
"TTL": "1800",
179+
"Type": "CNAME"
79180
}
80181
},
81182
"EmailIdentity7187767D": {
@@ -90,11 +191,10 @@
90191
"EmailIdentityMailFromMxRecordCEAAECD0": {
91192
"Type": "AWS::Route53::RecordSet",
92193
"Properties": {
93-
"Name": "mail.cdk.dev.",
94-
"Type": "MX",
95194
"HostedZoneId": {
96195
"Ref": "HostedZoneDB99F866"
97196
},
197+
"Name": "mail.cdk.dev.",
98198
"ResourceRecords": [
99199
{
100200
"Fn::Join": [
@@ -109,21 +209,22 @@
109209
]
110210
}
111211
],
112-
"TTL": "1800"
212+
"TTL": "1800",
213+
"Type": "MX"
113214
}
114215
},
115216
"EmailIdentityMailFromTxtRecordE6B5E5D0": {
116217
"Type": "AWS::Route53::RecordSet",
117218
"Properties": {
118-
"Name": "mail.cdk.dev.",
119-
"Type": "TXT",
120219
"HostedZoneId": {
121220
"Ref": "HostedZoneDB99F866"
122221
},
222+
"Name": "mail.cdk.dev.",
123223
"ResourceRecords": [
124224
"\"v=spf1 include:amazonses.com ~all\""
125225
],
126-
"TTL": "1800"
226+
"TTL": "1800",
227+
"Type": "TXT"
127228
}
128229
}
129230
},

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ses/test/integ.email-identity.js.snapshot/integ.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)