You can download the local DynamoDB library here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
This section shows how you can run locally the application,communicating with a local DynamoDB instance.
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory
aws dynamodb create-table --endpoint-url http://localhost:8000 --table-name songs --key-schema AttributeName=id,KeyType=HASH --attribute-definitions AttributeName=id,AttributeType=S --provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=100
mvn tomcat7:run -Dspring.profiles.active=dev
The application is available in the location: http://localhost:8080/
This section shows how you can deploy the application in the AWS cloud.
- Create the key from the AWS console and store the private key locally as
EC2_instance_key.pem - Restrict the permissions in the file
chmod 600 EC2_instance_key.pem
Execute the following command, after replacing the region & aws_account_id in the ec2_app_server_policy.json file
aws iam create-role --role-name EC2-AppServer-Role --assume-role-policy-document file://aws/ec2_trust.json
aws iam put-role-policy --role-name EC2-AppServer-Role --policy-name EC2-AppServer-Permissions --policy-document file://aws/ec2_app_server_policy.json
aws iam create-instance-profile --instance-profile-name EC2-AppServer-Instance-Profile
aws iam add-role-to-instance-profile --role-name EC2-AppServer-Role --instance-profile-name EC2-AppServer-Instance-Profile
Execute the following CLI commands:
aws ec2 create-security-group --description 'Security group for application servers' --group-name app-server-sg
aws ec2 authorize-security-group-ingress --group-id <security_group_id> --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id <security_group_id> --protocol tcp --port 8080 --cidr 0.0.0.0/0
- Using an AMI image that contains Java 8 + Maven
- Using the security group and the IAM role we previously created
aws dynamodb create-table --table-name songs --key-schema AttributeName=id,KeyType=HASH --attribute-definitions AttributeName=id,AttributeType=S --provisioned-throughput ReadCapacityUnits=100,WriteCapacityUnits=100
scp -r -i EC2_instance_key.pem <folder_to_the_package_locally> ec2-user@<ec2_domain_name>:/home/ec2-user
ssh -i EC2_instance_key.pem ec2-user@<ec2_domain_name>
mvn tomcat7:run -Dspring.profiles.active=prod
The application is available in the location http://<ec2_domain_name>:8080/
Execute the following commands, having in mind that you might have to use different S3 bucket names, if they exist:
aws s3api create-bucket --bucket com.packtpub.catalogservice.source
aws s3api create-bucket --bucket com.packtpub.catalogservice.artifacts
aws iam create-role --role-name CodeBuildServiceRole --assume-role-policy-document file://aws/code_build_role.json
aws iam put-role-policy --role-name CodeBuildServiceRole --policy-name CodeBuildServiceRolePolicy --policy-document file://aws/code_build_policy.json
aws kms create-key
aws kms get-key-policy --key-id <key_id> --policy-name default --output text > aws/kms_policy.json
{
"Sid": "Allow access through Amazon S3 for all principals in the account that are authorized to use Amazon S3",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"kms:ViaService": "s3.<region>.amazonaws.com",
"kms:CallerAccount": "<account-ID>"
}
}
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<account-ID>:role/CodeBuildServiceRole"
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
}
aws kms put-key-policy --key-id <kms_key_id> --policy-name default --policy file://aws/kms_policy.json
aws codebuild create-project --generate-cli-skeleton > aws/build_project.json
{
"name": "CatalogService_CodeBuild_project",
"description": "",
"source": {
"type": "S3",
"location": "com.packtpub.catalogservice.source/CatalogService.zip"
},
"artifacts": {
"type": "S3",
"location": "com.packtpub.catalogservice.artifacts"
},
"environment": {
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/java:openjdk-8",
"computeType": "BUILD_GENERAL1_SMALL"
},
"serviceRole": "CodeBuildServiceRole",
"encryptionKey": "<kms_key_arn>"
}
aws codebuild create-project --cli-input-json file://aws/build_project.json
zip -r target/CatalogService.zip .
aws s3 mv target/CatalogService.zip s3://com.packtpub.catalogservice.source/CatalogService.zip
aws codebuild start-build --project-name CatalogService_CodeBuild_project
Note: Before proceeding, you'll need to update the CodeBuild project from the previous section, so that artifacts packaging is set to ZIP.
This launch configuration will use the corresponding security group, IAM role, AMI and will have CodeDeploy agent installed
aws autoscaling create-launch-configuration --launch-configuration-name catalog-service-config --key-name EC2_instance_key --image-id ami-18e8ef72 --instance-type t2.small --iam-instance-profile EC2-AppServer-Instance-Profile --security-groups app-server-sg --user-data file://aws/ec2_user_data.sh
aws autoscaling create-auto-scaling-group --auto-scaling-group-name catalog-service-scaling-group --launch-configuration-name catalog-service-config --min-size 0 --max-size 3 --availability-zones us-east-1a
Warning: In a real production environment, it's unsafe to set the min capacity to 0 instances from the availability point of view.
aws autoscaling update-auto-scaling-group --auto-scaling-group-name catalog-service-scaling-group --desired-capacity 1
The file ec2_app_server_policy now also contains additional permissions for the S3 bucket, so that EC2 servers can download the artifacts. So, execute the following (after replacing the region & aws_account_id) to update the policy:
aws iam put-role-policy --role-name EC2-AppServer-Role --policy-name EC2-AppServer-Permissions --policy-document file://aws/ec2_app_server_policy.json
aws iam create-role --role-name CodeDeployServiceRole --assume-role-policy-document file://aws/code_deploy_trust_policy.json
aws iam attach-role-policy --role-name CodeDeployServiceRole --policy-arn arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole
aws deploy create-application --application-name CatalogServiceApp
aws deploy create-deployment-group \
--application-name CatalogServiceApp \
--auto-scaling-groups catalog-service-scaling-group \
--deployment-group-name CatalogServiceDG \
--deployment-config-name CodeDeployDefault.OneAtATime \
--service-role-arn <service-role-arn>
aws deploy create-deployment \
--application-name CatalogServiceApp \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name CatalogServiceDG \
--s3-location bucket=com.packtpub.catalogservice.artifacts,bundleType=zip,key=CatalogService_CodeBuild_project
aws s3api create-bucket --bucket com.packtpub.catalogservice.delivery.pipeline
aws s3api put-bucket-versioning --bucket com.packtpub.catalogservice.delivery.pipeline --versioning-configuration Status=Enabled
You will first have to edit the file code_pipeline_role_policy.json file and replace , <account_id> with the right values.
aws iam create-role --role-name CodePipelineRole --assume-role-policy-document file://aws/code_pipeline_trust_policy.json
aws iam put-role-policy --role-name CodePipelineRole --policy-name CodePipelineRolePolicy --policy-document file://aws/code_pipeline_role_policy.json
You will first have to edit the code_pipeline.json file and replace the <account_id> with your AWS account ID.
aws codepipeline create-pipeline --pipeline file://aws/code_pipeline.json
This is needed, because during deployments EC2 servers now need access to the artifacts from the CodePipeline.
Execute the following, after updating the values for the AWS account ID and region in the file ec2_app_server_policy.json.
aws iam put-role-policy --role-name EC2-AppServer-Role --policy-name EC2-AppServer-Permissions --policy-document file://aws/ec2_app_server_policy.json
You can download the protocol buffers compiler (protoc), in the following link: https://github.com/google/protobuf/releases
You can use the following command to generate source code for Java & Ruby from our catalog service protocol buffers definition
./protoc --java_out=generated-src/java --ruby_out=generated-src/ruby catalog_service.proto
First, create the SQS queue:
aws sqs create-queue --queue-name published_songs_queue
The file ec2_app_server_policy now also contains additional permissions for the SQS queue, so that EC2 servers can send messages there. So, execute the following (after replacing the region & aws_account_id) to update the policy:
aws iam put-role-policy --role-name EC2-AppServer-Role --policy-name EC2-AppServer-Permissions --policy-document file://aws/ec2_app_server_policy.json
The file ec2_app_server_policy now also contains additional permissions for Cloudwatch, so that EC2 servers can publish metrics. So, execute the following (after replacing the region & aws_account_id) to update the policy:
aws iam put-role-policy --role-name EC2-AppServer-Role --policy-name EC2-AppServer-Permissions --policy-document file://aws/ec2_app_server_policy.json
To generate load, you can use the the locust tool, which you can install with the following command:
pip install locust
To generate traffic to your host, use the following command, replacing the server ip with the Public IP of the EC2 server:
locust --host http://<ec2_server>:8080 -f ./load_gen/locustfile.py