CloudFormation Stack Configuration
CloudFormation Stack Configuration
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Spring Boot Cloudformation demo stack.",
"SpringBootApplicationVersion": {
"Type": "AWS::ElasticBeanstalk::ApplicationVersion",
"Properties": {
"ApplicationName":{"Ref":"SpringBootApplication"},
"SourceBundle": {
"S3Bucket": {"Ref":"SourceCodeBucket"},
"S3Key": "beanstalk-deployment-1.0-SNAPSHOT.jar"
}
}
}
"SpringBootBeanStalkConfigurationTemplate": {
"Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
"Properties": {
"ApplicationName": {"Ref":"SpringBootApplication"},
"Description":"A display of speed boot application",
"OptionSettings": [
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MinSize",
"Value": "2"
},
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MaxSize",
"Value": "2"
},
{
"Namespace": "aws:elasticbeanstalk:environment",
"OptionName": "EnvironmentType",
"Value": "LoadBalanced"
}
],
"SolutionStackName": "64bit Amazon Linux 2016.09 v2.3.0 running Java 8"
}
}
"SpringBootBeanstalkEnvironment": {
"Type": "AWS::ElasticBeanstalk::Environment",
"Properties": {
"ApplicationName": {"Ref":"SpringBootApplication"},
"EnvironmentName":"JavaBeanstalkEnvironment",
"TemplateName": {"Ref":"SpringBootBeanStalkConfigurationTemplate"},
"VersionLabel": {"Ref": "SpringBootApplicationVersion"}
}
}
6. Now deploy the cloudFormation template:
AWS Fargate:
AWS Fargate is an AWS managed service that is responsible for provisioning and
orchestrating your containerized application. This means that you can deploy hundreds of
containers without having to define any compute resources because the service will do it for
you.
Steps:
Build the application and build docker image.
Then push the image to AWS ECR or Dockerhub.
Sample Templates:
https://raw.githubusercontent.com/jasonumiker/nginx-codebuild/master/fargate-
cloudformation.template
https://github.com/1Strategy/fargate-cloudformation-example/blob/master/fargate.yaml
DynamoDB
Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that
need consistent, single-digit millisecond latency at any scale. It is a fully managed cloud
database and supports both document and key-value store models.
Maven Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.34</version>
</dependency>
<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>4.5.0</version>
</dependency>
application.properties:
amazon.dynamodb.endpoint=http://localhost:8000/
amazon.aws.accesskey=key
amazon.aws.secretkey=key2
create DynamoDBConfig class:
Model class:
@DynamoDBTable(tableName = "Book")
public class Book {
private String id;
private String name;
private String price;
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public String getId() {
return id;
}
@DynamoDBAttribute
public String getName() {
return name;
}
@DynamoDBAttribute
public String getPrice() {
return price;
}
}
Repository Interface:
@EnableScan
public interface BookRepository extends CrudRepository<Book, String> {
List<Book> findById(String id);
}
Testing spring application with DynamoDB:
we want to test our application to ensure that we can connect and perform operations
on our local DynamoDB:
Elastic cache
Amazon ElastiCache allows you to seamlessly set up, run, and scale popular open-
Source compatible in-memory data stores in the cloud. Elasticcache has support for two
open-source engines:
Redis
Memcached
Maven Dependencies:
application.properties:
redis.hostname=URL_TO_ELASTIC_CACHE_REDIS_CLUSTER
redis.port=6379
redis.prefix=testing
Implementing service:
@Cacheable(value = "getLongRunningTaskResult", key="{#seconds}",
cacheManager = "cacheManager1Hour")
public Optional<TaskDTO> getLongRunningTaskResult(long seconds) {
try {
long randomMultiplier = new Random().nextLong();
long calculatedResult = randomMultiplier * seconds;
TaskDTO taskDTO = new TaskDTO();
taskDTO.setCalculatedResult(calculatedResult);
Thread.sleep(2000); // 2 Second Delay to Simulate Workload
return Optional.of(taskDTO);
} catch (InterruptedException e) {
return Optional.of(null);
}
}
Testing: