The next microservice we need for our TravelBuddy serverless application is the FlightSpecials API. We have seen FlightSpecials before in this course, so it should be familiar to you. It functions the same way as the HotelSpecials API in that it must be deployed with VPC integration so it can connect to the MySQL database to query for data. Instead of manually packaging and deploying this microservice, we will use CodeStar to build out a full CI/CD pipeline for us, as we have done in previous labs. So, since you have created CI/CD pipelines using AWS CodeStar a few times now, we are not going to provide you with full step-by-step instructions. If you need help, ask a Lab Assistant how to complete the tasks required to create a new CodeStar project and deploy the FlightSpecials code over the sample application provided by CodeStar.
Contents
FlightSpecialsAPI
. If you need help with this task, see the CodeStar documentation.git config helpers
.new-implementation
copy_files.sh
script in the FlightSpecials.zip
bundle that you can use, as before in previous lab exercises. To use it, run the below command by replacing path/to/your/working/folder
E.G.:copy_files.sh /path/to/your/working/folder
Don’t forget to use the Maven | Update Project feature of the Eclipse IDE to ‘rethink’ the project after the change to the source code.
Give CloudFormation permission to create an IAM role
As part of our microservice setup, we will be assigning a new IAM Role to our Lambda function to allow it to perform various tasks. When CodeStar created our project, it created an IAM Role that gave CloudFormation just enough permissions to deploy the Hello World example service. These permissions are not enough for our more advanced requirements. So we need to adjust the policies assigned to the CloudFormation role, to extend those permissions.
CodeStarWorker-flightspecialsa-CloudFormation
to locate the correct IAM Role.If you can’t find the role, it may be too early - CodeStar may still be provisioning the pipeline and may not yet have created the role. Check the progress of the provisioning in the CodeStar dashboard.
idevelop
to shortlist the available policies. Select the idevelopCodeStarCloudFormationPolicy
policy shown in the list. This policy has been created for you by the lab setup process, and has the permissions assigned to it to allow CloudFormation to work on our behalf when creating the Lambda execution role.template.yml
that was provided as part of the FlightSpecials.zip
file has some placeholder values that you need to update to match the values from your lab account before you can deploy the updates. These include Subnet Ids, Security Group Ids and the RDS Instance Endpoint, which are unique to your lab account and unknown at this stage to the template.In the Eclipse IDE, open the template.yml
file and search for each of the items in the table below in the Replace column, and replace them with the values that are found in Cloudformation Outputs tab:
Item to replace | Description |
---|---|
<DatabaseSubnet1> | First network subnet for the mySQL database |
<DatabaseSubnet2> | Second network subnet for the mySQL database |
<DatabaseSecurityGroup> | The security group for the mySQL database |
<RDSEndpoint> | The mySQL database endpoint |
VpcConfig:
SecurityGroupIds:
- sg-858e61e3
SubnetIds:
- subnet-3c989d4a
- subnet-a04255c4
Environment:
Variables:
JDBC_CONNECTION_STRING: jdbc:mysql://qlxx5jzrxxxby.cig0rzmdedgf.ap-northeast-1.rds.amazonaws.com:3306/travelbuddy?useSSL=false&serverTimezone=UTC&autoReconnect=true
JDBC_UID: root
JDBC_PWD: labpassword
The swagger.yml
file provided in the zip bundle is the definition for the API that exposes the microservice via Amazon API Gateway. It needs to be updated with details of your lab AWS Account Id and target AWS Region before you can deploy your microservice.
swagger.yml
file and open it in the text editorREPLACE_AWS_REGION
and replace it with the region you are using (for example, ap-northeast-1
)REPLACE_AWS_ACCOUNTID
and replace it with the your AWS Account Id. You can find your AWS Account Id in the lab dashboard.new-implementation
git branch and commit the files. You can use the Eclipse IDE or the commandline. In this example, we will use the commandline, but note for the final push you will need to use the Eclipse IDE since it is configured with your Git credentials, whereas the commandline has not:
git status
to review the changed code filesgit add .
to add in the changed filesgit commit -m "Baseline implementation"
to commit the changes and provide a messagegit checkout master
to switch back to the master branchgit merge new-implementation
You need to perform the push from Eclipse because the git credentials are embedded within the Eclipse environment. You could also configure the command line environment with the git credentials but that is beyond the scope of this lab.
It will take a few moments to push the code and commence the deployment. Checking in the source code and templates will trigger the pipeline to build and deploy the new implementation. AWS CodePipeline will now use CloudFormation to deploy the following resources:
While you have some time, spend a few minutes to explore the buildspec.yml
, swagger.yml
and template.yml
files which define the Amazon API Gateway, AWS Lambda function and the build/deployment process through AWS CodeBuild. You will notice that the builspec.yml
file uses the same AWS CLI command to package the deployment that you used when you manually packaged the HotelSpecials API earlier.
Do not execute these commands below, they are here as a reference!
mvn package shade:shade
aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml
The template.yml
file uses the Serverless Application Model (SAM) transform to define the REST API, as opposed to how we approached this in the HotelSpecials API example. In HotelSpecials, the CloudFormation template used canonical CloudFormation to define the REST API so that it could be explicit about the API definition and include the swagger
definition inline. The FlightSpecials example uses a more declarative approach and left the details to SAM. You are free to mix CloudFormation and SAM in the same template, as you can see from these examples.
Note that the $S3_BUCKET
environment variable is automatically replaced by CodeBuild when the build step is performed, and replaced by the S3 Bucket that CodeStar provisioned when it set up the project.
You can check the status of the deployment of the code change through the pipeline back on the CodeStar project dashboard. Once the deployment has completed through the CI/CD pipeline, you are ready to test the API.
iDevelop - Flight Specials API
link to reveal the resources for the API.{
"succeeded": true,
"message": "",
"errorCode": 0,
"data": [
{
"id": 1,
"header": "London to Prague",
"body": "Jewel of the East",
"cost": 93,
"expiryDate": 1504072439813
},
{
"id": 2,
"header": "Paris to London",
"body": "Weekend getaway!",
"cost": 182,
"expiryDate": 1504074888702
}
]
}
If you see a JSON payload with no errors, you have successfully deployed an API and supporting Lambda function that queries the mySQL database. Notice how much quicker and easier that was to deploy without any manual intervention, and you didn’t even have to interact with the AWS CLI or Console? Everything was driven by the source control check-in process.
You are now ready to integrate these APIs with the TravelBuddy web site.