1

使用localstack我正在尝试创建一个执行以下操作的模板:

  1. 创建 SNS 主题
  2. 创建 SQS 队列
  3. 创建将 SQS 队列订阅到 SNS 主题的订阅。

我的docker-compose文件如下所示:

version: '3'

services:

  localstack:
    image: localstack/localstack
    container_name: localstack
    environment:
      - SERVICES=sns,sqs,cloudformation
      - DEBUG=1
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - HOSTNAME=localstack
      - AWS_DEFAULT_REGION=eu-west-2
      - AWS_ACCESS_KEY_ID=XX
      - AWS_SECRET_ACCESS_KEY=XX
    ports:
      - "4575:4575"
      - "4576:4576"
      - "4581:4581"
      - "8080:8080"
    volumes:
      - ./config/formation.yml:/usr/stuff/formation.yml
      - ./config/init.sh:/docker-entrypoint-initaws.d/init.sh

我的init.sh文件如下所示:

#!/bin/bash
aws cloudformation create-stack --stack-name fincorestack --template-body file:///usr/stuff/formation.yml --endpoint-url=http://localstack:4581

最后我的 Cloudformation 文件如下所示:

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Test'
Resources:
  MySnsTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: MySnsTopic
  MySnsTopicSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: sqs
      TopicArn: !Ref MySnsTopic
      Endpoint: !GetAtt
        - MySqsQueue
        - QueueArn
  MySqsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: MySqsQueue

这会产生一个非常模糊的500 Internal Server Error。因为这是localstack并且我知道 arns 是相当静态的,所以我尝试用以下内容替换yml订阅文件TopcArnEndpoint内容:

TopicArn: arn:aws:sns:eu-west-2:123456789012:MySnsTopic
Endpoint: arn:aws:sqs:elasticmq:000000000000:MySqsQueue

这次我没有收到错误,但没有创建订阅。从localstack我的调试输出中可以看到:

在此处输入图像描述

并且未创建订阅。Cloudformation执行中不支持此功能localstack还是我做错了什么?

4

4 回答 4

2

在 cli 中使用带有 localstack 的 docker ...

启动你的本地堆栈

创建主题:

AWS_ACCESS_KEY_ID=x AWS_SECRET_ACCESS_KEY=x aws --endpoint-url=http://dc-localstack:4566 --region us-east-1 sns create-topic --name jensTopic`

回复 :

{
    "SubscriptionArn": "arn:aws:sns:us-east-1:000000000000:jensTopic:9bf7628e-ee36-49c1-8216-ed5cf5aea1ed"
}

创建队列:

AWS_ACCESS_KEY_ID=x AWS_SECRET_ACCESS_KEY=x aws --endpoint=http://dc-localstack:4566 --region us-east-1 sqs create-queue --queue-name jensQueue

获取队列 arn 属性:

AWS_ACCESS_KEY_ID=x AWS_SECRET_ACCESS_KEY=x aws --endpoint-url=http://dc-localstack:4566 --region us-east-1 sqs get-queue-attributes --queue-url http://localhost:4566/queue/jensQueue --attribute-names QueueArn

回复:

{
    "Attributes": {
        "QueueArn": "arn:aws:sqs:us-east-1:000000000000:jensQueue"
    }
}

订阅队列到主题:

AWS_ACCESS_KEY_ID=x AWS_SECRET_ACCESS_KEY=x aws --endpoint-url=http://dc-localstack:4566 --region us-east-1 sns subscribe --topic-arn "arn:aws:sns:us-east-1:000000000000:jensTopic" --protocol sqs --notification-endpoint "arn:aws:sqs:us-east-1:000000000000:jensQueue"

完整性检查:

AWS_ACCESS_KEY_ID=x AWS_SECRET_ACCESS_KEY=x aws --endpoint-url=http://dc-localstack:4566 --region us-east-1 sns list-subscriptions

回复:

{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:us-east-1:000000000000:jensTopic:9bf7628e-ee36-49c1-8216-ed5cf5aea1ed",
            "Owner": "",
            "Protocol": "sqs",
            "Endpoint": "arn:aws:sqs:us-east-1:000000000000:jensQueue",
            "TopicArn": "arn:aws:sns:us-east-1:000000000000:jensTopic"
        }
    ]
}
于 2021-05-17T17:45:27.913 回答
1

我认为QueueArn不是 的属性AWS::SQS::Queue,它应该只是Arn

于 2019-03-12T19:52:38.397 回答
1

现在已修复此问题:https ://github.com/localstack/localstack/issues/1191

虽然TopcArn并且Endpoint仍然需要硬编码。

于 2019-10-24T08:15:34.087 回答
0

找到了一个解决方法,所以发布以防其他人需要这个。我有一种感觉,根本不支持将订阅创建为资源,localstack因此我在定义 SNS 主题资源时创建了它们。将文件更改yml为以下内容就可以了:

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Test'
Resources:
  MySqsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: MySqsQueue

  MySqsQueue:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: MySqsQueue
      Subscription:
        - Protocol: sqs
          Endpoint:
            "Fn::GetAtt": ["MySqsQueue", "Arn"]
          RawMessageDelivery: 'true'

编辑:

不幸的是,以这种方式创建订阅不允许设置属性。就我而言,我需要RawMessageDelivery=true不支持的(请参见此处)。很烦人...

于 2019-03-13T08:39:51.273 回答