Step 1 – Signing up with CloudAMQP
Acorn Services allow developers to easily provision databases, caches, storage buckets, and other as-a-service offerings from cloud providers and Kubernetes operators within the Acorn context.
In this tutorial, we will build an Acorn Service for provisioning a RabbitMQ instance within CloudAMQP, a hosted message queuing service in the cloud.
This guide has two parts: 1) Packaging RabbitMQ as an Acorn Service, and 2) Consuming the Acorn Service from other Acorns. You can read the second part here.
By the end of this tutorial, we will have a running Acorn Service that exposes RabbitMQ within the Acorn environment.
Visit CloudAMQP.com and complete the signup process. CloudAMQP has a free tier that can provision RabbitMQ instances without needing a credit card.
Once logged in, visit the API Access section and create an API key. This is useful for programmatically managing the service.
Save the API key before proceeding with the next steps. Initializing an environment variable with the API key is a good idea.
Let’s create an Acorn Secret for the API Key. This will be used by the Service to manage the instance’s lifecycle.
acorn secret create cloudamqp-account --data api_key=$API_KEY
It’s time to define the Acorn Service in an Acornfile.
Let’s take a look at each section of the Acornfile.
The args section initializes a few defaults that can be changed during the runtime. The name of the service, the plans supported by CloudAMQP, and the AWS region parameters are defined here.
The next section, services, defines the actual service – rabbitmq-cloudamqp-server – by pointing it to the job that does the initialization and lifecycle management of the RabbitMQ instance.
The jobs section is a classic Acorn Job that builds an artifact from the Dockerfile and passes the environment variables gathered from the command line arguments and the external secret created in the previous step.
The next section is critical. It refers to the external secret storing the CloudAMQP API key and the secrets generated by the service at runtime. The second secret becomes available at runtime to other Acorns that consume the service.
For more information on Acorn Services, refer to the documentation.
With the Acornfile in place, it’s time to create the actual job responsible for the lifecycle of the RabbitMQ instance.
Create a file named render.sh in a directory called scripts.
The script receives the parameters as environment variables during the runtime and executes the curl commands to provision and terminate the RabbitMQ instance. The events create and delete call the appropriate sections of the script to manage the lifecycle. For example, when acorn rm is called, it invokes the delete_rabbitmq function to terminate the instance.
In the parent folder, where the Acornfile is located, create a Dockerfile with the below content:
FROM python:alpine3.9 RUN apk add --no-cache curl jq ADD . /acorn ENTRYPOINT [ "/acorn/scripts/render.sh" ]
This Dockerfile becomes a part of the Acorn Job, which is a part of the Acorn Service.
Let’s build, push, and run the Acorn Service. If everything goes well, running the service should create a RabbitMQ instance at CloudAMQP.
acorn build -t index.docker.io/janakiramm/rabbitmq-service:latest .
After logging into Docker Hub, push the image.
acorn push index.docker.io/janakiramm/rabbitmq-service:latest
Finally, let’s run the Acorn Service with the defaults and check if the RabbitMQ instance is created.
acorn run -n rabbitmq-01 index.docker.io/janakiramm/rabbitmq-service:latest
We can see the instance running within CloudAMQP. In the next part of this tutorial, we will build Acorns that act as a publisher and subscriber of the RabbitMQ instance running as an Acorn Service.
In this tutorial we’ve used the Acorn Services capabilities to create a service that provisions a RabbitMQ instance using CloudAMQP. In the second part of this post, we demonstrate how to leverage this service in a sample application. Acorn Services can be used to connect to any cloud service and incorporate it into your Acorn ecosystem. You can explore tutorials for other services, such as NATS and MongoDB Atlas.