- Newest
- Most votes
- Most comments
To set up automatic acceptance for transit gateway peering attachments using AWS CDK TypeScript, you'll need to implement a custom solution as there isn't a built-in CDK construct for this specific functionality. Here's an approach you can take:
-
Create the transit gateway peering attachment as you've done in your existing code.
-
Use a Custom Resource with a Lambda function to automatically accept the peering attachment. The Lambda function will use the AWS SDK to call the
acceptTransitGatewayPeeringAttachmentAPI. -
Add a dependency to ensure the Custom Resource is created after the peering attachment.
Here's how you can modify your existing code to implement this:
- First, create a Lambda function that will accept the peering attachment:
const acceptPeeringFunction = new lambda.Function(this, 'AcceptPeeringFunction', { runtime: lambda.Runtime.NODEJS_14_X, handler: 'index.handler', code: lambda.Code.fromAsset('lambda'), timeout: Duration.seconds(30), role: new iam.Role(this, 'AcceptPeeringFunctionRole', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), managedPolicies: [ iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'), ], }), }); acceptPeeringFunction.addToRolePolicy(new iam.PolicyStatement({ actions: ['ec2:AcceptTransitGatewayPeeringAttachment'], resources: ['*'], }));
- Create a Custom Resource that uses this Lambda function:
const acceptPeeringCustomResource = new CustomResource(this, 'AcceptPeeringCustomResource', { serviceToken: acceptPeeringFunction.functionArn, properties: { TransitGatewayAttachmentId: tgwPeering.attrTransitGatewayAttachmentId, }, });
- Add a dependency to ensure the Custom Resource is created after the peering attachment:
acceptPeeringCustomResource.node.addDependency(tgwPeering);
- Implement the Lambda function (in a separate file, e.g., 'lambda/index.js'):
const AWS = require('aws-sdk'); exports.handler = async (event, context) => { const ec2 = new AWS.EC2(); const { TransitGatewayAttachmentId } = event.ResourceProperties; if (event.RequestType === 'Create' || event.RequestType === 'Update') { try { await ec2.acceptTransitGatewayPeeringAttachment({ TransitGatewayAttachmentId }).promise(); return { PhysicalResourceId: TransitGatewayAttachmentId, Data: { Message: 'Peering attachment accepted' } }; } catch (error) { throw new Error(`Failed to accept peering attachment: ${error.message}`); } } else if (event.RequestType === 'Delete') { // No action needed on delete return { PhysicalResourceId: TransitGatewayAttachmentId }; } };
This solution creates a Custom Resource that uses a Lambda function to automatically accept the transit gateway peering attachment. The Lambda function is triggered when the Custom Resource is created or updated, and it calls the acceptTransitGatewayPeeringAttachment API to accept the peering attachment.
Remember to handle error cases and ensure proper IAM permissions are set for the Lambda function to interact with EC2 services. Also, note that this approach assumes both transit gateways are in the same AWS account and region. If they're in different accounts or regions, you'll need to adjust the Lambda function accordingly.
Sources
Amazon EC2 examples using AWS CLI - AWS SDK Code Examples
AcceptTransitGatewayPeeringAttachment - Amazon Elastic Compute Cloud
interface CfnTransitGatewayPeeringAttachmentProps · AWS CDK
