Per the tutorial instructions you should be able to just run the Python setup script (setup.py), Run “python setup.py -m setup -r
And unfortunately it failed. I received the error
Regarding line numbers throughout this post. They may be off by a bit given either changes to the original script made by authors or my revisions causing shifts in the numbers. You may need to perform string searches to find the exact lines. Note: I have also included a github link at the bottom of the post to my modified script for you to download
It’s pretty clear that a python module is missing. Interstingly Boto3 is the AWS SDK for python. To install Boto3 I simply ran
remember I have multiple versions of python on my system so you may just need to use pip instead of the pip3 command
After I successfully installed the boto3 module I then attempted the setup again. This time I received an error from the botocore.
If you look at the documentation for boto3 , you will see that you need proper AWS credentials setup on your local machine in order for the AWS python SDK to be able to communicate to services within your AWS account. Yes if you haven’t set up an Amazon account yet, you will need to sign up for at least a free tier account. Disclaimer there will be incurred costs if you leave these cloud formation stacks up and running. The cloud formation stack is using a RDS and relatively large containers. You will probably want to spin up the stack do some testing and tear down the stack. Or be prepared to pay some momey to Amazon. The EC2 instance types defined in the cloud formation are C4.xlarge and M4.large across the different modules. You probably want to swtich this to t2.micro while youa re playing with with the cloudformation stack to try and minimize AWS expenses. You can always increase the sizes later. In the version of the script I posted to GitHub I have changed the EC2 instances to t2.micro instances. If you attempt to do this on your own you will want to change the memory allocation for the EC2 instances that are defined in the python setup script as well. It is set at 1024. Your stack will never spin up appropriately as a t2 maxes out at 1GB of memory. I was seeing a 503 error from the load balancer whenever I tried to spin up the stack. I changed the allocation to 512.
I wanted to avoid using my root administrator account, so I logged into AWS console. After logging in, you should see a dropdown in the top menu for services. The IAM (Identity Access Management) service link should be available in the far left panel if you have accessed it recently or you could search for IAM.
Now proceed to create a new user that will be used to create a cloud formation stack. The user will be used by python scripts and the python AWS SDK that defines the cloud formation stack and all the defined services. Click the Users link to get to the Users page. You should see a button at the top, “Add User”. You will brought through a multi-step wizard to create the user. I created a “dev_user”, created a custom DevGroup, and assigned the following roles to the group:
- PowerUerAccess
- IAMFullAccess
- AmazonEC2ContainerRegistryFullAccess
When the user is created you will see the key items you need for configuration on your local system: the Access Key and the Secret Access Key. Please note the warning that AWS provides. This will be the only time you will be able to see / download the secret access key. Make sure you have a means to get this key into your config.
If you read through the Boto3 configuration documentation you will see there are multiple options for the credential configuration. I chose to use the shared credential file. You need to vi the credentials file that will be present under .aws directory under your user’s home directory.
Then add the Access Key and Shared Secret Key you downloaded or noted from the user creation within your AWS console under the [default] profile within the aws credentials file.
Now I was ready to attempt to run the setup script once again.
This execution cycle I received the following error:
There are obviously some things wrong with the script. I have a feeling it has to do with recent revs of Python or Boto3. I didn’t spend time trying to figure out what reved and caused the issue, I focused on finding what changes I could make to get the script working.
It is somewhat clear from the last line that the script is encountering a bytestring as indicated by the b’ prefix to the hostname string, but it is expecting a string. At line 497 of the script I changed the lines
to
Ok. Time to give the setup script another whirl. Progress, I now saw the login to AWS succeeding and the docker configuration being created. But now it failed at the point of interacting with the AWS ECR (Elastic Container Registry).
Well a similar issue but in reverse this time. Here I made a line change at line 543. Change
to
Is the 3rd time a charm?
Unfortunately not. It looks like items are in a bit of an incomplete state.
It appears there is already an item in the Elastic Container Registry (ECR) for the spring-petclinic-rest project. The previous runs of the steup.py script has probably created some artifacts and have left things in an incomplete state. I went and checked the ECR service through the AWS console and sure enough there was an existing entry for the spring-petclinic-rest container. So I decided to see how well the cleanup option on the script would work even in an incomplete state.
I didn’t see anything in the log output to indicate that the registry was cleaned up, but when I went back to the AWS console and performed a refresh, I saw that the there were no longer any registries nor containers defined. Please note that a delete of a cloud formation stack can take quite awile 15 - 20 minutes. The script does continuously check the “DELETE_IN_PROGRESS” state and eventually should complete.
So now I went back to my terminal and ran the setup script once again. I ran into a few more problems with expecting strings and getting bytestrings or vice versa. Here are the additional lines that needed to be modifed. Line 605 changed from
to
And Line 623 changed from
to
And Line 639 changed from
to
And Line 663 changed from
to
And Line 585 changed from
to
And Line 857 changed from
to
There was also an issue of
This is an issue of going from Python 2 to Python 3. You might want to read this post reagarding the change https://blog.labix.org/2008/06/27/watch-out-for-listdictkeys-in-python-3
Line 648 changed from
to
This issue was also present at Line 687
changed to
Eventually I got to a point were the script was completing successfully. Similar to the delete stack process you may see the Create stack (status: CREATE_IN_PROGRESS) take a prolonged time to complete.
After the the setup was finally completed I could then open my preferred browser and hit the various endpoints. The endpoint as indicated in the log output (AWS ELB url) plus one of the below:
/ /pet /vet /owner /visit All worked appropriately and provide a result. The / will bring you to a ‘Welcome to PetClinic’ page. The others will provide a json response.
Source of modified script (hosted on GitHub): setup.py