Skip to content

Commit bb8d6f6

Browse files
authored
fix(aws-appsync): Strip unsupported characters from Lambda DataSource (#18765)
Closes #16169 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a1d94b3 commit bb8d6f6

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/@aws-cdk/aws-appsync/lib/data-source.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export abstract class BaseDataSource extends CoreConstruct {
111111
if (extended.type !== 'NONE') {
112112
this.serviceRole = props.serviceRole || new Role(this, 'ServiceRole', { assumedBy: new ServicePrincipal('appsync') });
113113
}
114-
const name = props.name ?? id;
114+
// Replace unsupported characters from DataSource name. The only allowed pattern is: {[_A-Za-z][_0-9A-Za-z]*}
115+
const name = (props.name ?? id).replace(/[\W]+/g, '');
115116
this.ds = new CfnDataSource(this, 'Resource', {
116117
apiId: props.api.apiId,
117118
name: name,

packages/@aws-cdk/aws-appsync/test/appsync-lambda.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ describe('Lambda Data Source configuration', () => {
6565
});
6666
});
6767

68+
test('appsync sanitized datasource name from unsupported characters', () => {
69+
const badCharacters = [...'!@#$%^&*()+-=[]{}\\|;:\'",<>?/'];
70+
71+
badCharacters.forEach((badCharacter) => {
72+
// WHEN
73+
const newStack = new cdk.Stack();
74+
const graphqlapi = new appsync.GraphqlApi(newStack, 'baseApi', {
75+
name: 'api',
76+
schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.test.graphql')),
77+
});
78+
const dummyFunction = new lambda.Function(newStack, 'func', {
79+
code: lambda.Code.fromAsset(path.join(__dirname, 'verify/iam-query')),
80+
handler: 'iam-query.handler',
81+
runtime: lambda.Runtime.NODEJS_12_X,
82+
});
83+
graphqlapi.addLambdaDataSource(`data-${badCharacter}-source`, dummyFunction);
84+
85+
// THEN
86+
Template.fromStack(newStack).hasResourceProperties('AWS::AppSync::DataSource', {
87+
Type: 'AWS_LAMBDA',
88+
Name: 'datasource',
89+
});
90+
});
91+
});
92+
93+
test('appsync leaves underscore untouched in datasource name', () => {
94+
// WHEN
95+
api.addLambdaDataSource('data_source', func);
96+
97+
// THEN
98+
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::DataSource', {
99+
Type: 'AWS_LAMBDA',
100+
Name: 'data_source',
101+
});
102+
});
103+
68104
test('appsync errors when creating multiple lambda data sources with no configuration', () => {
69105
// THEN
70106
expect(() => {

0 commit comments

Comments
 (0)