项目作者: anthonyrabiaza

项目描述 :
Mule Application for API configuration on AWS ELB Target Group
高级语言: Java
项目地址: git://github.com/anthonyrabiaza/aws-elb-system-api.git
创建时间: 2017-11-16T07:36:54Z
项目社区:https://github.com/anthonyrabiaza/aws-elb-system-api

开源协议:

下载


How-to automate AWS ELB to expose APIs

This is a set of sample to automate the binding of Mule APIs with AWS ELB.

  1. A AWS System API which will configure AWS Target Group
  2. An example of an API using AWS ELB (source here: https://github.com/anthonyrabiaza/aws-elb-dummy-api)

Getting Started

Please pull the sources of the two Mule Project from github.

You have to configure and deploy aws-elb-system-api and update your API following the example aws-elb-dummy-api.

Prerequisites

Yon need to have the following elements:

  • A AWS Elastic Load Balancer (Application Load Balancer)
  • A Target Group

AWS Application ELB:
alt text

AWS Target Group:
alt text

THe AWS Target Group can have an existing configuration or not:
alt text

For any change in the project, you would need to setup Maven and a have a Maven account from MuleSoft. See https://github.com/anthonyrabiaza/cicd-dummy

Deploying the System API aws-elb-system-api

This Mule Application is responsible of registering and deregistering the APIs using AWS ELB.
You can deploy multiple versions of this Application if required.

Before deploying the AWS, please update the mule-app.properties file:

  1. aws.elb.region=ap-southeast-1
  2. aws.elb.targetgroup.arn=arn:aws:elasticloadbalancing:ap-southeast-1:492498145701:targetgroup/elb-tg/33ad2a22f37ff3cb
  3. aws.elb.targetgroup.port=8091
  4. aws_access_key_id=<<ID from Security Credentials Access keys>>
  5. aws_secret_access_key=<<Secret from Security Credentials>>

The first two parameters are related to the AWS ELB components: the AWS Region and the ARN of the AWS ELB (previous screen).

The third parameter is the port exposed by the API (in your example 8091):

  • 8091 if the API is exposing service via HTTP from VPC
  • 8092 if the API is exposing service via HTTPS from VPC

Once deployed, please note the endpoint of the Application, it can be:

Implementing the OnStartupOnShutdown ELB Event in your API

Copy the following elements from the example aws-elb-dummy-api to your API Project:

  • The flow aws-elb-OnStartupOnShutdown.xml

alt text

  • The Java classes

alt text

Once the copy made, you have add the following property to mule-app.properties:

  • For a Cloudhub deployment (in Singapore), you would need to add mule-worker-internal- to the previous noted endpoint:
    1. aws-elb-system-api.url=http://mule-worker-internal-aws-elb-system-api-ant.au.cloudhub.io:8091/api/event
  • For an Hybrid deployment:
    1. aws-elb-system-api.url=http://edge:8091/api/event

Checking the events

Once the AWS System API is configured and deployed. You can deploy an API and check the logs of both of the applications.

Start of API

  • API logs
  1. INFO com.mulesoft.mule.tools.startup.OnStartupOnShutdown - ## IS READY ACTIVITIES
  2. INFO com.mulesoft.mule.tools.startup.ELBEvent - Calling AWS ELB System API (http://mule-worker-internal-aws-elb-system-api-ant.au.cloudhub.io:8091/api/event?apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=start)
  3. INFO com.mulesoft.mule.tools.startup.ELBEvent - Response code:200
  4. INFO com.mulesoft.mule.tools.startup.OnStartupOnShutdown - ## END OF IS READY ACTIVITIES
  • AWS System API logs:
  1. INFO org.mule.api.processor.LoggerMessageProcessor -
  2. org.mule.DefaultMuleMessage
  3. {
  4. id=64c91b30-ca82-11e7-8083-000c296d8c98
  5. payload=org.mule.transport.NullPayload
  6. correlationId=<not set>
  7. correlationGroup=-1
  8. correlationSeq=-1
  9. encoding=UTF-8
  10. exceptionPayload=<not set>
  11. Message properties:
  12. INVOCATION scoped properties:
  13. _ApikitResponseTransformer_AcceptedHeaders=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  14. _ApikitResponseTransformer_apikitRouterRequest=yes
  15. _ApikitResponseTransformer_contractMimeTypes=[]
  16. INBOUND scoped properties:
  17. accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  18. connection=keep-alive
  19. content-length=0
  20. content-type=application/x-www-form-urlencoded
  21. host=mule-worker-internal-aws-elb-system-api-ant.au.cloudhub.io:8091
  22. http.listener.path=/api/*
  23. http.method=POST
  24. http.query.params=ParameterMap{[apiName=[aws-elb-dummy-api], ipAddress=[10.0.238.252,127.0.0.1], state=[start]]}
  25. http.query.string=apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=start
  26. http.relative.path=/api/event
  27. http.remote.address=/10.0.238.252:39995
  28. http.request.path=/api/event
  29. http.request.uri=/api/event?apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=start
  30. http.scheme=http
  31. http.uri.params=ParameterMap{[]}
  32. http.version=HTTP/1.1
  33. user-agent=Java/1.8.0_121
  34. OUTBOUND scoped properties:
  35. SESSION scoped properties:
  36. }

If the Application is not deployed in a VPC with an IP starting with 10. the first IP will be used and the following message will be displayed:

  1. ERROR com.mulesoft.mule.tools.aws.elb.ConfigureELBTargetGroup - No valid ip found in list (192.168.0.168,127.0.0.1), using first one

You can see the update of the configuration in AWS Target Group:
alt text

Stop of API

  • API logs

    1. INFO com.mulesoft.mule.tools.startup.OnStartupOnShutdown - ## SHUTDOWN ACTIVITIES
    2. INFO com.mulesoft.mule.tools.startup.ELBEvent - Calling AWS ELB System API (http://mule-worker-internal-aws-elb-system-api-ant.au.cloudhub.io:8091/api/event?apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=stop)
    3. INFO com.mulesoft.mule.tools.startup.ELBEvent - Response code:200
    4. INFO com.mulesoft.mule.tools.startup.OnStartupOnShutdown - ## END OF SHUTDOWN ACTIVITIES
  • AWS System API logs:

  1. INFO org.mule.api.processor.LoggerMessageProcessor -
  2. org.mule.DefaultMuleMessage
  3. {
  4. id=cdcd7f20-ca84-11e7-8083-000c296d8c98
  5. payload=org.mule.transport.NullPayload
  6. correlationId=<not set>
  7. correlationGroup=-1
  8. correlationSeq=-1
  9. encoding=UTF-8
  10. exceptionPayload=<not set>
  11. Message properties:
  12. INVOCATION scoped properties:
  13. _ApikitResponseTransformer_AcceptedHeaders=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  14. _ApikitResponseTransformer_apikitRouterRequest=yes
  15. _ApikitResponseTransformer_contractMimeTypes=[]
  16. INBOUND scoped properties:
  17. accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  18. connection=keep-alive
  19. content-length=0
  20. content-type=application/x-www-form-urlencoded
  21. host=mule-worker-internal-aws-elb-system-api-ant.au.cloudhub.io:8091
  22. http.listener.path=/api/*
  23. http.method=POST
  24. http.query.params=ParameterMap{[apiName=[aws-elb-dummy-api], ipAddress=[10.0.238.252,127.0.0.1], state=[stop]]}
  25. http.query.string=apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=stop
  26. http.relative.path=/api/event
  27. http.remote.address=/10.0.238.252:40204
  28. http.request.path=/api/event
  29. http.request.uri=/api/event?apiName=aws-elb-dummy-api&ipAddress=10.0.238.252,127.0.0.1&state=stop
  30. http.scheme=http
  31. http.uri.params=ParameterMap{[]}
  32. http.version=HTTP/1.1
  33. user-agent=Java/1.8.0_121
  34. OUTBOUND scoped properties:
  35. SESSION scoped properties:
  36. }

You can see the update of the configuration in AWS Target Group:
alt text

If the Application is not deployed in a VPC with an IP starting with 10. the first IP will be used and the following message will be displayed:

  1. ERROR com.mulesoft.mule.tools.aws.elb.ConfigureELBTargetGroup - No valid ip found in list (192.168.0.168,127.0.0.1), using first one