Search code examples
amazon-web-servicesaws-api-gatewayaws-cdktelegram-webhook

Working with the generated API Gateway url in CDK


I'm creating a Telegram bot using AWS API Gateway and AWS Lambda, and defining the infrastructure using AWS CDK v2.

After deploying my stack with cdk deploy I get the URL that has been generated for the API Gateway in the console output. Ideally, I would like to use that URL to register a webhook in Telegram as part of the deployment process. This would just involve making a GET request to an URL, passing a secret from AWS Secrets Manager and the generated URL for the gateway.

Is there any way to perform that HTTP request during the deployment within CDK?


Solution

  • The way to do this would be to create a cloudformation custom resource to handle the request after the api gateway is up. the cdk would look something like

    const api = new apigateway.RestApi(this, 'Api', {
          restApiName: 'MyApi',
    
    });
    
    const provider = new cr.Provider(this, 'Provider', {
            onEventHandler: new nodeLambda.NodejsFunction(this, 'handler', {
                handler: 'handler',
                }),
    });
    
    new cdk.CustomResource(this, 'registerApi', {
      serviceToken: provider.serviceToken,
      properties: {
          apiGatewayUrl: api.url,
      },
    });
    
    

    Your lambda code would get the api url from the resource properties passed in the event, then get your secrets from secrets manager, and make the call to telegram to register you're new api