Skip to content

Commit 0c1aeb6

Browse files
authored
feat(apprunner): add kmsKey property for the AppRunner Service class (#30352)
### Issue # (if applicable) Close #30365. ### Reason for this change AppRunner supports for using a customer managed key to encrypt all stored copies of your application source image or source bundle. https://docs.aws.amazon.com/apprunner/latest/dg/security-data-protection-encryption.html But L2 Construct (alpha module) cannot use a customer managed key. ### Description of changes Add kmsKey property to the Service class. ### Description of how you validated changes Add unit tests and integ tests ### 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 5f229ce commit 0c1aeb6

12 files changed

+710
-3
lines changed

packages/@aws-cdk/aws-apprunner-alpha/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The `Service` construct allows you to create AWS App Runner services with `ECR P
3232
- `Source.fromEcr()` - To define the source repository from `ECR`.
3333
- `Source.fromEcrPublic()` - To define the source repository from `ECR Public`.
3434
- `Source.fromGitHub()` - To define the source repository from the `Github repository`.
35-
- `Source.fromAsset()` - To define the source from local asset directory.
35+
- `Source.fromAsset()` - To define the source from local asset directory.
3636

3737

3838
The `Service` construct implements `IGrantable`.
@@ -183,7 +183,7 @@ new apprunner.Service(this, 'Service', {
183183
## Secrets Manager
184184

185185
To include environment variables integrated with AWS Secrets Manager, use the `environmentSecrets` attribute.
186-
You can use the `addSecret` method from the App Runner `Service` class to include secrets from outside the
186+
You can use the `addSecret` method from the App Runner `Service` class to include secrets from outside the
187187
service definition.
188188

189189
```ts
@@ -216,6 +216,24 @@ const service = new apprunner.Service(stack, 'Service', {
216216
service.addSecret('LATER_SECRET', apprunner.Secret.fromSecretsManager(secret, 'field'));
217217
```
218218

219+
## Use a customer managed key
220+
221+
To use a customer managed key for your source encryption, use the `kmsKey` attribute.
222+
223+
```ts
224+
import * as kms from 'aws-cdk-lib/aws-kms';
225+
226+
declare const kmsKey: kms.IKey;
227+
228+
new apprunner.Service(this, 'Service', {
229+
source: apprunner.Source.fromEcrPublic({
230+
imageConfiguration: { port: 8000 },
231+
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
232+
}),
233+
kmsKey,
234+
});
235+
```
236+
219237
## HealthCheck
220238

221239
To configure the health check for the service, use the `healthCheck` attribute.

packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ecr from 'aws-cdk-lib/aws-ecr';
22
import * as assets from 'aws-cdk-lib/aws-ecr-assets';
33
import * as iam from 'aws-cdk-lib/aws-iam';
4+
import * as kms from 'aws-cdk-lib/aws-kms';
45
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
56
import * as ssm from 'aws-cdk-lib/aws-ssm';
67
import * as cdk from 'aws-cdk-lib/core';
@@ -79,7 +80,7 @@ export class Cpu {
7980
*
8081
* @param unit The unit of CPU.
8182
*/
82-
private constructor(public readonly unit: string) {}
83+
private constructor(public readonly unit: string) { }
8384
}
8485

8586
/**
@@ -715,6 +716,13 @@ export interface ServiceProps {
715716
* @default - no health check configuration
716717
*/
717718
readonly healthCheck?: HealthCheck;
719+
720+
/**
721+
* The customer managed key that AWS App Runner uses to encrypt copies of the source repository and service logs.
722+
*
723+
* @default - Use an AWS managed key
724+
*/
725+
readonly kmsKey?: kms.IKey;
718726
}
719727

720728
/**
@@ -1239,6 +1247,9 @@ export class Service extends cdk.Resource implements iam.IGrantable {
12391247
this.renderCodeConfiguration(this.source.codeRepository!.codeConfiguration.configurationValues!) :
12401248
undefined,
12411249
},
1250+
encryptionConfiguration: this.props.kmsKey ? {
1251+
kmsKey: this.props.kmsKey.keyArn,
1252+
} : undefined,
12421253
networkConfiguration: {
12431254
egressConfiguration: {
12441255
egressType: this.props.vpcConnector ? 'VPC' : 'DEFAULT',

packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-encryption.js.snapshot/AppRunnerEncryptionDefaultTestDeployAssert21640739.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-encryption.js.snapshot/AppRunnerEncryptionDefaultTestDeployAssert21640739.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-encryption.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-encryption.js.snapshot/integ-apprunner-encryption.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"Resources": {
3+
"Key961B73FD": {
4+
"Type": "AWS::KMS::Key",
5+
"Properties": {
6+
"KeyPolicy": {
7+
"Statement": [
8+
{
9+
"Action": "kms:*",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"AWS": {
13+
"Fn::Join": [
14+
"",
15+
[
16+
"arn:",
17+
{
18+
"Ref": "AWS::Partition"
19+
},
20+
":iam::",
21+
{
22+
"Ref": "AWS::AccountId"
23+
},
24+
":root"
25+
]
26+
]
27+
}
28+
},
29+
"Resource": "*"
30+
}
31+
],
32+
"Version": "2012-10-17"
33+
}
34+
},
35+
"UpdateReplacePolicy": "Delete",
36+
"DeletionPolicy": "Delete"
37+
},
38+
"ServiceInstanceRoleDFA90CEC": {
39+
"Type": "AWS::IAM::Role",
40+
"Properties": {
41+
"AssumeRolePolicyDocument": {
42+
"Statement": [
43+
{
44+
"Action": "sts:AssumeRole",
45+
"Effect": "Allow",
46+
"Principal": {
47+
"Service": "tasks.apprunner.amazonaws.com"
48+
}
49+
}
50+
],
51+
"Version": "2012-10-17"
52+
}
53+
}
54+
},
55+
"ServiceDBC79909": {
56+
"Type": "AWS::AppRunner::Service",
57+
"Properties": {
58+
"EncryptionConfiguration": {
59+
"KmsKey": {
60+
"Fn::GetAtt": [
61+
"Key961B73FD",
62+
"Arn"
63+
]
64+
}
65+
},
66+
"InstanceConfiguration": {
67+
"InstanceRoleArn": {
68+
"Fn::GetAtt": [
69+
"ServiceInstanceRoleDFA90CEC",
70+
"Arn"
71+
]
72+
}
73+
},
74+
"NetworkConfiguration": {
75+
"EgressConfiguration": {
76+
"EgressType": "DEFAULT"
77+
}
78+
},
79+
"ServiceName": "service",
80+
"SourceConfiguration": {
81+
"AuthenticationConfiguration": {},
82+
"AutoDeploymentsEnabled": false,
83+
"ImageRepository": {
84+
"ImageConfiguration": {
85+
"Port": "8000"
86+
},
87+
"ImageIdentifier": "public.ecr.aws/aws-containers/hello-app-runner:latest",
88+
"ImageRepositoryType": "ECR_PUBLIC"
89+
}
90+
}
91+
}
92+
}
93+
},
94+
"Outputs": {
95+
"URL": {
96+
"Value": {
97+
"Fn::Join": [
98+
"",
99+
[
100+
"https://",
101+
{
102+
"Fn::GetAtt": [
103+
"ServiceDBC79909",
104+
"ServiceUrl"
105+
]
106+
}
107+
]
108+
]
109+
}
110+
}
111+
},
112+
"Parameters": {
113+
"BootstrapVersion": {
114+
"Type": "AWS::SSM::Parameter::Value<String>",
115+
"Default": "/cdk-bootstrap/hnb659fds/version",
116+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
117+
}
118+
},
119+
"Rules": {
120+
"CheckBootstrapVersion": {
121+
"Assertions": [
122+
{
123+
"Assert": {
124+
"Fn::Not": [
125+
{
126+
"Fn::Contains": [
127+
[
128+
"1",
129+
"2",
130+
"3",
131+
"4",
132+
"5"
133+
],
134+
{
135+
"Ref": "BootstrapVersion"
136+
}
137+
]
138+
}
139+
]
140+
},
141+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
142+
}
143+
]
144+
}
145+
}
146+
}

packages/@aws-cdk/aws-apprunner-alpha/test/integ.service-encryption.js.snapshot/integ.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)