Project description
This project aims to create an application with microservices architecture using Spring for all development. For virtualization, Docker and Localstack is used for provides an easy-to-use test/mocking framework for developing Cloud applications.
Here is the complete code of the project
Installation and Technologies
The following technologies were used to carry out the project and it is necessary to install some items:
- Docker
- Java 17
- Maven
- LocalStack
- Spring
- AWScli
- DynamoDB
- S3
LocalStack
LocalStack is an open-source mock of the real AWS services. s a cloud service emulator that runs in a single container on your laptop or in your CI environment. With LocalStack, you can run your AWS applications or Lambdas entirely on your local machine without connecting to a remote cloud provider! Whether you are testing complex CDK applications or Terraform configurations, or just beginning to learn about AWS services, LocalStack helps speed up and simplify your testing and development workflow.
Benefits LocalStack
- Reduce Cost
- Test AWS Cloud Resource Locally
- Learn AWS Cloud Resource Locally
Services Supported by LocalStack
LocalStack spins up the following core Cloud APIs on your local machine.
- ACM
- API Gateway
- CloudFormation
- CloudWatch
- CloudWatch Logs
- DynamoDB
- DynamoDB Streams
- EC2
- Elasticsearch Service
- EventBridge (CloudWatch Events)
- Firehose
- IAM
- Kinesis
- KMS
- Lambda
- Redshift
- Route53
- S3
- SecretsManager
- SES
- SNS
- SQS
- SSM
- StepFunctions
- STS
Requirements
- python (3.6 or higher)
- pip (Python package manager)
- Docker
For installing you used this command:
pip install localstack
After installing, if you view the list with all services available, you used this command:
localstack status services
About Project
The project simulate using AWS resources: DynamoDB and S3. In this project you have a directory with docker-compose.yml
version: '3.9'
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack
ports:
- "4566-4599:4566-4599"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- SERVICES=dynamodb,s3
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
- DOCKER_HOST=unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- localstack_data:/tmp/localstack/data
networks:
- localstack
volumes:
localstack_data:
networks:
localstack: {}
This file you prepared all tools necessary for your application, running everything with command:
docker-compose up -d
You need add in your this dependency in your repository:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.175</version>
</dependency>
after adding the library, you can perform the following functions in DynamoDB:
- Create connection
- Using DynamoDB with LocalStack
You used the code block below for create one instance to this database in DynamoDB with Java
@Bean
public AmazonDynamoDB amazonDynamoDB(){
return AmazonDynamoDBClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4566", "any region"))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
.build();
}
For you to create your database in dynamodb, you must follow the following code blocks
private static CreateTableRequest getCreateTableRequest(){
return new CreateTableRequest()
.withAttributeDefinitions(new AttributeDefinition("name", ScalarAttributeType.S))
.withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
.withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))
.withTableName("MyTable");
}
And the last step
public static void main(String[] args) {
final AmazonDynamoDB amazonDynamoDBClient = getAmazonDynamoDBClient();
final CreateTableRequest request = getCreateTableRequest();
try {
CreateTableResult result = amazonDynamoDBClient.createTable(request);
System.out.println(result.getTableDescription().getTableName());
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
}
}
To create your bucket on S3, you will need to run the following command:
aws s3api --endpoint-url=http://localhost:4566 create-bucket --bucket test
Top comments (0)