使用AWS CloudFormation创建SNS平台应用程序



是否可以使用云形式模板创建SNS平台应用程序?

支持aws-cli,http://docs.aws.amazon.com/cli/latest/reference/sns/create-platform-application.html。但是,没有关于对云形式进行相同操作的信息,是否完全支持(http://docs.aws.aws.amazon.com/awscloudformation/latest/userguide/aws-properties-sns-sns-topic.html)?

no 。它是不是使用AWS中的CloudFormation创建SNS平台应用程序。

我面临着同样的问题。在AWS中,可以编号文档,今天只有AWS::SNS::SubscriptionAWS::SNS::TopicAWS::SNS::TopicPolicy。他们都没有允许定义平台应用程序。

除非可以在另一个AWS::服务下声明,否则今天不可能。希望很快就能提供此功能。

一种解决方案

•由于创建/更新应用程序端点是一个罕见的"事件",每年一次,我将手动:(创建它们。

awscliaws-sdk似乎支持平台应用程序的创建,但它会添加额外的依赖性或开发

值得添加一些额外的工作来获得自动化和可重复性的好处。

我使用 AWS::CloudFormation::CustomResource和lambda功能来完成工作。

将其添加到您的云形式定义中。

"MyPlatformApplication": {
    Type: "AWS::CloudFormation::CustomResource",
    DependsOn: [ "PlatformApplicationProvisionLambdaFunction" ],
    Properties: {
        ServiceToken: { "Fn::GetAtt", "PlatformApplicationProvisionLambdaFunction.Arn" },
        Name: "MyPlatformApplication",
        Platform: "GCM",
        PlatformCredential: "<Your FCM ServerKey>",
    },
},

并使用您的首选策略来定义lambda函数PlatformApplicationProvisionLambdaFunction。对我来说,我使用无服务器框架,以便我可以将上述自定义资源与lambda函数放在同一存储库中,然后将所有这些都放在一起。

这是打字稿中的lambda代码

import {
    CreatePlatformApplicationCommand,
    DeletePlatformApplicationCommand,
    SetPlatformApplicationAttributesCommand,
    SNSClient,
} from '@aws-sdk/client-sns';
import {
    CloudFormationCustomResourceCreateEvent,
    CloudFormationCustomResourceDeleteEvent,
    CloudFormationCustomResourceEvent,
    CloudFormationCustomResourceUpdateEvent,
    Context,
} from 'aws-lambda';
import response from 'cfn-response';
import middy from '@middy/core';
import middyJsonBodyParser from '@middy/http-json-body-parser';
const snsClient = new SNSClient({});
const parseInput = (event: CloudFormationCustomResourceEvent) => {
    const input = {
        Name: event.ResourceProperties.Name,
        Platform: event.ResourceProperties.Platform,
        PlatformCredential: event.ResourceProperties.PlatformCredential,
    };
    if (input.Name === undefined) {
        throw new Error('Missing parameter: Name');
    }
    if (input.Platform === undefined) {
        throw new Error('Missing parameter: Platform');
    }
    if (input.PlatformCredential === undefined) {
        throw new Error('Missing parameter: PlatformCredential');
    }
    return input;
};
const processCreate = async (event: CloudFormationCustomResourceCreateEvent, context: Context) => {
    const input = parseInput(event);
    const result = await snsClient.send(
        new CreatePlatformApplicationCommand({
            Name: input.Name,
            Platform: input.Platform,
            Attributes: {
                PlatformCredential: input.PlatformCredential,
            },
        }),
    );
    response.send(event, context, 'SUCCESS', {
        Arn: result.PlatformApplicationArn,
    });
};
const processUpdate = async (event: CloudFormationCustomResourceUpdateEvent, context: Context) => {
    const input = parseInput(event);
    const arn = `arn:aws:sns:${process.env.REGION}:${process.env.ACCOUNT_ID}:app/${input.Platform}/${input.Name}`;
    await snsClient.send(
        new SetPlatformApplicationAttributesCommand({
            PlatformApplicationArn: arn,
            Attributes: {
                PlatformCredential: input.PlatformCredential,
            },
        }),
    );
    response.send(event, context, 'SUCCESS', {
        Arn: arn,
    });
};
const processDelete = async (event: CloudFormationCustomResourceDeleteEvent, context: Context) => {
    const input = parseInput(event);
    const arn = `arn:aws:sns:${process.env.REGION}:${process.env.ACCOUNT_ID}:app/${input.Platform}/${input.Name}`;
    await snsClient.send(
        new DeletePlatformApplicationCommand({
            PlatformApplicationArn: arn,
        }),
    );
    response.send(event, context, 'SUCCESS', {
        Arn: arn,
    });
};
const handler = async (event: CloudFormationCustomResourceEvent, context: Context) => {
    try {
        if (event.RequestType === 'Create') {
            await processCreate(event, context);
        } else if (event.RequestType === 'Update') {
            await processUpdate(event, context);
        } else {
            await processDelete(event, context);
        }
    } catch (err) {
        if (err instanceof Error) {
            response.send(event, context, 'FAILED', { message: err.message });
        } else {
            response.send(event, context, 'FAILED', err);
        }
    }
};
export const main = middy(handler).use(middyJsonBodyParser());

最新更新