Let's jump into a brief example of App Mesh using Spring Boot
We'll start by setting up the basic infrastructure for our services. All commands will be provided as if run from the same directory as this README.
You'll need a keypair stored in AWS to access a bastion host. You can create a keypair using the command below if you don't have one. See Amazon EC2 Key Pairs.
aws ec2 create-key-pair --key-name color-app | jq -r .KeyMaterial > ~/.ssh/color-app.pem
chmod go-r ~/.ssh/color-app.pem
This command creates an Amazon EC2 Key Pair with name color-app
and saves the private key at
~/.ssh/color-app.pem
.
Next, we need to set a few environment variables before provisioning the
infrastructure. Please change the value for AWS_ACCOUNT_ID
, KEY_PAIR_NAME
, and ENVOY_IMAGE
below.
export AWS_ACCOUNT_ID=<your account id>
export KEY_PAIR_NAME=<color-app or your SSH key pair stored in AWS>
export AWS_DEFAULT_REGION=us-west-2
export ENVIRONMENT_NAME=AppMeshSpringBoot
export MESH_NAME=ColorApp-SpringBoot
export ENVOY_IMAGE=<get the latest from https://docs.aws.amazon.com/app-mesh/latest/userguide/envoy.html>
export SERVICES_DOMAIN="default.svc.cluster.local"
export GATEWAY_IMAGE_NAME="gateway"
export COLOR_TELLER_IMAGE_NAME="colorteller"
First, create the VPC.
./infrastructure/vpc.sh
Next, create the ECS cluster and ECR repositories.
./infrastructure/ecs-cluster.sh
./infrastructure/ecr-repositories.sh
Finally, build and deploy the color app images.
./colorteller/deploy.sh
./gateway/deploy.sh
This mesh will be a simplified version of the original Color App Example, so we'll only be deploying the gateway and one color teller service (white).
Let's create the mesh.
./mesh/mesh.sh up
Our final step is to deploy the service and test it out.
./infrastructure/ecs-service.sh
Let's issue a request to the color gateway.
COLORAPP_ENDPOINT=$(aws cloudformation describe-stacks \
--stack-name $ENVIRONMENT_NAME-ecs-service \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="ColorAppEndpoint") | .OutputValue')
curl "${COLORAPP_ENDPOINT}/color"
BASTION_IP=$(aws cloudformation describe-stacks \
--stack-name $ENVIRONMENT_NAME-ecs-cluster \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="BastionIP") | .OutputValue')
ssh -i ~/.ssh/$KEY_PAIR_NAME.pem ec2-user@$BASTION_IP
You should see a successful response with the color white.
If you want to keep the application running, you can do so, but this is the end of this walkthrough. Run the following commands to clean up and tear down the resources that we’ve created.
aws cloudformation delete-stack --stack-name $ENVIRONMENT_NAME-ecs-service
aws cloudformation delete-stack --stack-name $ENVIRONMENT_NAME-ecs-cluster
aws cloudformation delete-stack --stack-name $ENVIRONMENT_NAME-mesh
aws ecr delete-repository --force --repository-name colorteller
aws ecr delete-repository --force --repository-name gateway
aws cloudformation delete-stack --stack-name $ENVIRONMENT_NAME-ecr-repositories
aws cloudformation delete-stack --stack-name $ENVIRONMENT_NAME-vpc