Skip to content

Commit

Permalink
fix(appsync): rds data source configured with cluster arn (#12255)
Browse files Browse the repository at this point in the history
Lazily add a formatted cluster arn to the RdsHttpEndpointConfig. Before we were just adding the cluster id, but the [docs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-appsync-datasource-rdshttpendpointconfig.html) call for the Cluster Arn instead. 

Fixes: #11536

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
BryanPan342 authored Jan 13, 2021
1 parent 37d8ccc commit d0305f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/@aws-cdk/aws-appsync/lib/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Grant, IGrantable, IPrincipal, IRole, Role, ServicePrincipal } from '@a
import { IFunction } from '@aws-cdk/aws-lambda';
import { IDatabaseCluster } from '@aws-cdk/aws-rds';
import { ISecret } from '@aws-cdk/aws-secretsmanager';
import { IResolvable, Stack } from '@aws-cdk/core';
import { IResolvable, Lazy, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { BaseAppsyncFunctionProps, AppsyncFunction } from './appsync-function';
import { CfnDataSource } from './appsync.generated';
Expand Down Expand Up @@ -318,17 +318,25 @@ export class RdsDataSource extends BackedDataSource {
relationalDatabaseConfig: {
rdsHttpEndpointConfig: {
awsRegion: props.databaseCluster.stack.region,
dbClusterIdentifier: props.databaseCluster.clusterIdentifier,
dbClusterIdentifier: Lazy.string({
produce: () => {
return Stack.of(this).formatArn({
service: 'rds',
resource: `cluster:${props.databaseCluster.clusterIdentifier}`,
});
},
}),
awsSecretStoreArn: props.secretStore.secretArn,
},
relationalDatabaseSourceType: 'RDS_HTTP_ENDPOINT',
},
});
props.secretStore.grantRead(this);
const clusterArn = Stack.of(this).formatArn({
service: 'rds',
resource: `cluster:${props.databaseCluster.clusterIdentifier}`,
});
props.secretStore.grantRead(this);

// Change to grant with RDS grant becomes implemented
Grant.addToPrincipal({
grantee: this,
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ describe('Rds Data Source configuration', () => {
});
});

test('rds cluster arn saved to RdsHttpEndpointConfig', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret);

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
RelationalDatabaseConfig: {
RdsHttpEndpointConfig: {
AwsRegion: { Ref: 'AWS::Region' },
AwsSecretStoreArn: { Ref: 'AuroraSecret41E6E877' },
DbClusterIdentifier: {
'Fn::Join': ['', ['arn:',
{ Ref: 'AWS::Partition' },
':rds:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':cluster:',
{ Ref: 'AuroraCluster23D869C0' }]],
},
},
},
});
});

test('default configuration produces name identical to the id', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret);
Expand Down

0 comments on commit d0305f3

Please sign in to comment.