- Edit the
FlightSpecials/template_cloud9.yml
and update the required parameters
If you are using eclipse you will need to have the CodeDeployRole as part of the parameters.
Item to replace |
Description |
|
First network subnet for the mySQL database |
|
Second network subnet for the mySQL database |
|
The security group for the mySQL database |
|
The mySQL database endpoint |
When you’ve completed updating the values, the VPCConfig section will look similar to this (with different values):
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
Update the target AWS region in the swagger.yml API definition file
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.
- In the Cloud9 IDE, locate the
swagger.yml
file and open it in the text editor
- Search for the text
REPLACE_AWS_REGION
and replace it with the region you are using (for example, ap-northeast-1
)
- Search for the text
REPLACE_AWS_ACCOUNTID
and replace it with the your AWS Account Id. You can find your AWS Account Id in the lab dashboard.
- Save the file.
- When bundling the zip file to be used in codedeploy, you need to bundle from the source directory. To do this run the following:
cd ~/environment/FlightSpecials
zip -r ../FlightSpecials.zip *
- Rezip the
FlightSpecials.zip
file with these updates.
cd ~/environment
rm FlightSpecials.zip
cd FlightSpecials
zip -r FlightSpecials.zip *
adding: buildspec.yml (deflated 53%)
adding: copy_files.sh (deflated 63%)
adding: pom.xml (deflated 75%)
adding: src/ (stored 0%)
adding: src/test/ (stored 0%)
adding: src/test/java/ (stored 0%)
adding: src/test/java/devlounge/ (stored 0%)
adding: src/test/java/devlounge/lambda/ (stored 0%)
adding: src/test/java/devlounge/lambda/test/ (stored 0%)
adding: src/test/java/devlounge/lambda/test/TestContext.java (deflated 76%)
adding: src/test/java/devlounge/lambda/test/TestUtils.java (deflated 79%)
adding: src/test/java/devlounge/lambda/test/LambdaFunctionHandlerTest.java (deflated 57%)
adding: src/main/ (stored 0%)
adding: src/main/java/ (stored 0%)
adding: src/main/java/devlounge/ (stored 0%)
adding: src/main/java/devlounge/model/ (stored 0%)
adding: src/main/java/devlounge/model/FlightSpecial.java (deflated 73%)
adding: src/main/java/devlounge/model/LambdaResult.java (deflated 62%)
adding: src/main/java/devlounge/lambda/ (stored 0%)
adding: src/main/java/devlounge/lambda/FlightSpecialsHandler.java (deflated 65%)
adding: swagger.yml (deflated 68%)
adding: template-configuration.json (deflated 18%)
adding: template.yml (deflated 65%)
Note that the directory FlightSpecials is now omitted which is very important to only have the actual code there that is required.
Create CodeStar Project
- Copy the code over to the FlightSpecials. Your lab bucket will look something like this
mod-9c10d0af24f74a4a-s3bucketlambdacode-vsuckr33vysr
aws s3 cp FlightSpecials.zip s3://<Lambdabucket>
- Copy the
toolchain.yml
to the Lamdba bucket
aws s3 cp toolchain.yml s3://<Lambdabucket>
- Create the codepipeline template for flightspecials, which will look like this:
Change the XXX with your account details below for this to work.
{
"name": "flightspecials",
"id": "flightspecials",
"description": "flight specials java8 example using ci/cd",
"sourceCode": [
{
"source": {
"s3": {
"bucketName": "<Lambdabucket>",
"bucketKey": "FlightSpecials.zip"
}
},
"destination": {
"codeCommit": {
"name": "flightspecials"
}
}
}
],
"toolchain": {
"source": {
"s3": {
"bucketName": "<Lambdabucket>",
"bucketKey": "toolchain.yml"
}
},
"roleArn": "arn:aws:iam::XXX:role/service-role/aws-codestar-service-role",
"stackParameters": {
"ProjectId": "flightspecials"
}
}
}
- Create the AWS Codestar project:
aws codestar create-project --cli-input-json file://template.json
{
"arn": "arn:aws:codestar:ap-northeast-1:436753246579:project/flightspecials"
}
Now check the cloudformation has completed, the codecommit has created a repo, and the first build has been kicked off.
If any of these fail, look at the errors. You will need to remove the created s3 buckets and recreate if there are any issues. If you are changing any code for FlightSpecials.zip
remember to resend this to the lambda bucket, and not just on the filesystem.
To see if all worked : aws codestar describe-project --id flightspecials
AWS Cloud9 setup codecommit
Once you have created this, you need to setup your required git repo for codecommit you configured previously.
- Move the original zip file, as this was just for the first upload, then setup required helper class and clone your repository.
mv FlightSpecials FlightSpecials_orig
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/flightspecials
cd flightspecials
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.
- Open the AWS IAM Console in the browser.
- Click Roles on the navigation pane.
- In the Filter box, type
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.
- Click on the role to open the details panel.
- Click Attach Policy, in the Filter box, type
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.
- Select the listed policy and click Attach Policy
Check changes into source control to trigger a deploy
- Now that you have completed updating files, you need to add the changed files to your
new-implementation
git branch and commit the files. You can use the commandline in your Cloud9 IDE:
- Issue
git status
to review the changed code files
- Issue
git add .
to add in the changed files
- Issue
git commit -m "Baseline implementation"
to commit the changes and provide a message
- Issue
git checkout master
to switch back to the master branch
- Now merge the changes for your new implementation into the master branch by issuing
git merge new-implementation
- Issue
git push
to push the changes to CodeCommit.
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.
Test the FlightSpecials API
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.
- Open the API Gateway console in the browser and under APIs, click the
iDevelop - Flight Specials API
link to reveal the resources for the API.
- Click on the Stages link beneath the iDevelop - Flight Specials API.
- Expand the prod root element in the Stages list to reveal the hierarchy.
- Click on the GET method
- Click on the Invoke URL value in the prod - GET - /flightspecials panel. After a moment while the Lambda function is initialised, you should see the JSON result of querying the mySQL database from the Lambda function. For example:
{
"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.