2
votes

I use a CF Template to create Beanstalk environments. I would like to trigger a Lambda code via SNS when an environment gets created so I can use the lambda to trigger a jenkins job with integration tests for the new environment.

Is there a way to send an SNS message after an env gets successfully created in Beanstalk? I already defined a topic the lambda code is subscribed to.

The beanstalk API allows you to define a notification endpoint. http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.sns.html

But what is this for if I can only specify an email address and I have to confirm subscription? How can I use it to trigger an SNS message automatically?

      {
        "OptionName": "Notification Endpoint",
        "Namespace": "aws:elasticbeanstalk:sns:topics",
        "Value": "[email protected]"
      },
      {
        "OptionName": "Notification Protocol",
        "Namespace": "aws:elasticbeanstalk:sns:topics",
        "Value": "email"
      },

A workaround I found is a bit hacky. I create an EC2 instance at the end of the stack creation process and run some AWS commands to send an SNS notification via UserData shell. Is this the only way?

2
Be aware that the JSON syntax that appears in this post does NOT agree with the syntax on the latest Amazon reference document: (docs.aws.amazon.com/elasticbeanstalk/latest/dg/…). You have ("OptionName", "Namespace", "Value"); they have ("option_name", "namespace", "value"). This seems like a ticket to insanity.doer

2 Answers

2
votes

I'm answering my own question. I managed to figure out how to use SNS to trigger lambda code after a new Beanstalk env gets created.

I created an sns topic service-configurator

and added its ARN and name to the template.

    {
      "OptionName": "Notification Topic ARN",
      "Namespace": "aws:elasticbeanstalk:sns:topics",
      "Value": "arn:aws:sns:us-east-1:273218181234:service-configurator"
    },
    {
      "OptionName": "Notification Topic Name",
      "Namespace": "aws:elasticbeanstalk:sns:topics",
      "Value": "service-configurator"
    }

Next, I set the sns topic to be an event source for my lambda code.

Now, lambda gets triggered every time something happens to an environment ( instances added/removed, env created etc.)

1
votes

While Configuring Notifications with Elastic Beanstalk does not provide a specific example for sending Amazon SNS notifications, email is simply the default for the resp. AWS Elastic Beanstalk option setting and you can also create subscriptions for most/all other protocols, see option aws:elasticbeanstalk:sns:topics:

  • Valid Values: http https email email-json sqs

Obviously AWS Lambda is not referenced there yet, but it is just another SNS protocol, so I would assume/hope that the table has simply not been updated yet and something like the following should just work accordingly (haven't tried it myself yet though):

  {
    "OptionName": "Notification Endpoint",
    "Namespace": "aws:elasticbeanstalk:sns:topics",
    "Value": "<Your Lambda function ARN>"
  },
  {
    "OptionName": "Notification Protocol",
    "Namespace": "aws:elasticbeanstalk:sns:topics",
    "Value": "lambda"
  },