Skip to content

Commit

Permalink
Sample updated to SpringBoot 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gortazar committed Jan 25, 2019
1 parent cf80f86 commit 801b619
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
18 changes: 18 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
Expand All @@ -27,5 +28,22 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
5 changes: 5 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
Expand Down
69 changes: 58 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ The application allows users to create ads with a picture. Ads are stored in a R

## Prerequisites

Some configurations are required in your AWS account for this sample to work. Basically, an _S3 bucket_ (by default `spring-cloud-aws-sample` is used, but it can be changed using `cloud.aws.s3.bucket` property), and an _RDS MySQL instance_ open to the world. Additionally, we need an _IAM user_ with access key and programmatic access to AWS API so that we can access AWS resources from our development machine.

### Create an IAM User

* Enable programmatic access
* Generate an access key for the user
* Give the user the following permissions:
** AmazonS3FullAccess
** AmazonRDSFullAccess

### Create an RDS Instance

**WARNING**: These instructions allow you to run and test the application from within your development environment (i.e., without deploying it to AWS) using an RDS instance open to the world, which is something you should avoid in production.
Expand Down Expand Up @@ -58,15 +48,72 @@ Finally, create an S3 bucket, name it `spring-cloud-aws-sample` and give read pe
]
}

### To run locally

**Note**: I was unable to run new versions of spring-cloud-aws locally. It seems it tries always to perform some autoconfiguration assuming it is deployed on AWS, and as long as it doesn't find a valid instance id, it raises an exception and stops. What follows here is the approach that worked for version 0.0.1 of the tutorial, useless in version 0.1.0. If you have an idea of how this can be solved, please fill in an issue and I'll look into it.

Some configurations are required in your AWS account for this sample to work. Basically, an _S3 bucket_ (by default `spring-cloud-aws-sample` is used, but it can be changed using `cloud.aws.s3.bucket` property), and an _RDS MySQL instance_ open to the world. Additionally, we need an _IAM user_ with access key and programmatic access to AWS API so that we can access AWS resources from our development machine.

#### Create an IAM User

* Enable programmatic access
* Generate an access key for the user
* Give the user the following permissions:
** AmazonS3FullAccess
** AmazonRDSFullAccess

### To run on EC2

#### Create an IAM role

Create an IAM role with the following properties:

* EC2 role (i.e., a role to be attached to EC2 instances)
* Policies:
** AmazonS3FullAccess
** AmazonRDSFullAccess

#### Create an EC2 instance

It has been tested with an instance with the following properties:

* AMI: Ubuntu 18.04
* Type: t2.micro
* Storage: 20Gb
* Security group: choose or create one with ports 22 and 8080 opened
* Attach the IAM role created previously

Once the instance has been started, ssh'd into the machine and issue the following commands:

```
sudo apt-get update
sudo apt-get install openjdk-8-jre-headless
```

Then from your own machine, build the jar file and upload it to your EC2 instance:

```
mvn package -DskipTests
scp -i <your key> spring-cloud-aws-sample-0.1.0.jar ubuntu@<your ec2 ip>:/home/ubuntu/
```

## Run the application

### Locally

For the impatient...

git clone https://github.com/codeurjc/spring-cloud-aws-sample
cd spring-cloud-aws-sample
mvn package
cd target
java -jar spring-cloud-aws-sample-0.0.1-SNAPSHOT.jar --cloud.aws.rds.dbInstanceIdentifier=springaws --cloud.aws.rds.springaws.password=<your pass> --cloud.aws.rds.springaws.username=springaws --cloud.aws.rds.springaws.databaseName=springaws --cloud.aws.credentials.accessKey="key" --cloud.aws.credentials.secretKey="secret"

### On an EC2 instance

If your EC2 instance has the appropriate role (see prerequisites above), and the jar file has been uploaded, the it can be run just by issuing:

java -jar spring-cloud-aws-sample-0.1.0-SNAPSHOT.jar --cloud.aws.rds.db-instance-identifier="springaws" --cloud.aws.rds.springaws.password="your rds password"




8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

<groupId>es.urjc.code</groupId>
<artifactId>spring-cloud-aws-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<version>2.1.2.RELEASE</version>
<relativePath />
</parent>

Expand All @@ -37,7 +37,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-autoconfigure</artifactId>
</dependency>
Expand Down Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;

@Controller
public class TablonController {
Expand Down Expand Up @@ -55,7 +56,7 @@ public String nuevoAnuncio(Model model,
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());

TransferManager transferManager = new TransferManager(s3);
TransferManager transferManager = TransferManagerBuilder.defaultTransferManager();
transferManager.upload(bucket, filename, file.getInputStream(), objectMetadata);

} catch (Exception e) {
Expand All @@ -79,7 +80,7 @@ public String nuevoAnuncio(Model model,
@RequestMapping("/anuncio/{id}")
public String verAnuncio(Model model, @PathVariable long id) {

Anuncio anuncio = repository.findOne(id);
Anuncio anuncio = repository.findById(id).get();

model.addAttribute("hasFoto", anuncio.getFoto() != null);
model.addAttribute("anuncio", anuncio);
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create

# Spring cloud properties (can be specified at start time as --cloud.aws.region.static=eu-west-1
# Make html the default extension for mustache
spring.mustache.suffix=.html

# Spring cloud properties (can be specified at start time as --cloud.aws.region.static=eu-west-1)

# Region (can be discovered dynamically when running within an EC2 instance
cloud.aws.region.static=eu-west-1

# Avoid trying to autodetect the stack when running in a manually deployed infrastructure
cloud.aws.stack.auto=false

# Credentials (unnecessary when running in an EC2 instance with a role having enough permissions for S3 and RDS)
#cloud.aws.credentials.accessKey="key"
#cloud.aws.credentials.secretKey="secret"

# In AWS use this instead, and create an EC2 role
#instanceProfile=true
instanceProfile=false

# RDS access
cloud.aws.rds.dbInstanceIdentifier=springaws
Expand Down

0 comments on commit 801b619

Please sign in to comment.