Skip to content

Commit 801b619

Browse files
committed
Sample updated to SpringBoot 2
1 parent cf80f86 commit 801b619

File tree

6 files changed

+96
-19
lines changed

6 files changed

+96
-19
lines changed

.classpath

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<attributes>
1616
<attribute name="optional" value="true"/>
1717
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
1819
</attributes>
1920
</classpathentry>
2021
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
@@ -27,5 +28,22 @@
2728
<attribute name="maven.pomderived" value="true"/>
2829
</attributes>
2930
</classpathentry>
31+
<classpathentry kind="src" path="target/generated-sources/annotations">
32+
<attributes>
33+
<attribute name="optional" value="true"/>
34+
<attribute name="maven.pomderived" value="true"/>
35+
<attribute name="ignore_optional_problems" value="true"/>
36+
<attribute name="m2e-apt" value="true"/>
37+
</attributes>
38+
</classpathentry>
39+
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
40+
<attributes>
41+
<attribute name="optional" value="true"/>
42+
<attribute name="maven.pomderived" value="true"/>
43+
<attribute name="ignore_optional_problems" value="true"/>
44+
<attribute name="m2e-apt" value="true"/>
45+
<attribute name="test" value="true"/>
46+
</attributes>
47+
</classpathentry>
3048
<classpathentry kind="output" path="target/classes"/>
3149
</classpath>

.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
2328
<buildCommand>
2429
<name>org.eclipse.m2e.core.maven2Builder</name>
2530
<arguments>

README.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ The application allows users to create ads with a picture. Ads are stored in a R
66

77
## Prerequisites
88

9-
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.
10-
11-
### Create an IAM User
12-
13-
* Enable programmatic access
14-
* Generate an access key for the user
15-
* Give the user the following permissions:
16-
** AmazonS3FullAccess
17-
** AmazonRDSFullAccess
18-
199
### Create an RDS Instance
2010

2111
**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.
@@ -58,15 +48,72 @@ Finally, create an S3 bucket, name it `spring-cloud-aws-sample` and give read pe
5848
]
5949
}
6050

51+
### To run locally
52+
53+
**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.
54+
55+
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.
56+
57+
#### Create an IAM User
58+
59+
* Enable programmatic access
60+
* Generate an access key for the user
61+
* Give the user the following permissions:
62+
** AmazonS3FullAccess
63+
** AmazonRDSFullAccess
64+
65+
### To run on EC2
66+
67+
#### Create an IAM role
68+
69+
Create an IAM role with the following properties:
70+
71+
* EC2 role (i.e., a role to be attached to EC2 instances)
72+
* Policies:
73+
** AmazonS3FullAccess
74+
** AmazonRDSFullAccess
75+
76+
#### Create an EC2 instance
77+
78+
It has been tested with an instance with the following properties:
79+
80+
* AMI: Ubuntu 18.04
81+
* Type: t2.micro
82+
* Storage: 20Gb
83+
* Security group: choose or create one with ports 22 and 8080 opened
84+
* Attach the IAM role created previously
85+
86+
Once the instance has been started, ssh'd into the machine and issue the following commands:
87+
88+
```
89+
sudo apt-get update
90+
sudo apt-get install openjdk-8-jre-headless
91+
```
92+
93+
Then from your own machine, build the jar file and upload it to your EC2 instance:
94+
95+
```
96+
mvn package -DskipTests
97+
scp -i <your key> spring-cloud-aws-sample-0.1.0.jar ubuntu@<your ec2 ip>:/home/ubuntu/
98+
```
99+
61100
## Run the application
62101

102+
### Locally
103+
63104
For the impatient...
64105

65106
git clone https://github.com/codeurjc/spring-cloud-aws-sample
66107
cd spring-cloud-aws-sample
67108
mvn package
68109
cd target
69110
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"
111+
112+
### On an EC2 instance
113+
114+
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:
115+
116+
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"
70117

71118

72-
119+

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
<groupId>es.urjc.code</groupId>
77
<artifactId>spring-cloud-aws-sample</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
8+
<version>0.1.0-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<parent>
1212
<groupId>org.springframework.boot</groupId>
1313
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>1.4.3.RELEASE</version>
14+
<version>2.1.2.RELEASE</version>
1515
<relativePath />
1616
</parent>
1717

@@ -37,7 +37,7 @@
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-data-jpa</artifactId>
3939
</dependency>
40-
<dependency>
40+
<dependency>
4141
<groupId>org.springframework.cloud</groupId>
4242
<artifactId>spring-cloud-aws-autoconfigure</artifactId>
4343
</dependency>
@@ -66,7 +66,7 @@
6666
<dependency>
6767
<groupId>org.springframework.cloud</groupId>
6868
<artifactId>spring-cloud-dependencies</artifactId>
69-
<version>Camden.SR3</version>
69+
<version>Greenwich.RELEASE</version>
7070
<type>pom</type>
7171
<scope>import</scope>
7272
</dependency>

src/main/java/es/urjc/code/daw/tablonanuncios/TablonController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.amazonaws.services.s3.AmazonS3;
1616
import com.amazonaws.services.s3.model.ObjectMetadata;
1717
import com.amazonaws.services.s3.transfer.TransferManager;
18+
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
1819

1920
@Controller
2021
public class TablonController {
@@ -55,7 +56,7 @@ public String nuevoAnuncio(Model model,
5556
ObjectMetadata objectMetadata = new ObjectMetadata();
5657
objectMetadata.setContentType(file.getContentType());
5758

58-
TransferManager transferManager = new TransferManager(s3);
59+
TransferManager transferManager = TransferManagerBuilder.defaultTransferManager();
5960
transferManager.upload(bucket, filename, file.getInputStream(), objectMetadata);
6061

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

82-
Anuncio anuncio = repository.findOne(id);
83+
Anuncio anuncio = repository.findById(id).get();
8384

8485
model.addAttribute("hasFoto", anuncio.getFoto() != null);
8586
model.addAttribute("anuncio", anuncio);

src/main/resources/application.properties

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
spring.datasource.driverClassName=com.mysql.jdbc.Driver
22
spring.jpa.hibernate.ddl-auto=create
33

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

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

12+
# Avoid trying to autodetect the stack when running in a manually deployed infrastructure
13+
cloud.aws.stack.auto=false
14+
915
# Credentials (unnecessary when running in an EC2 instance with a role having enough permissions for S3 and RDS)
1016
#cloud.aws.credentials.accessKey="key"
1117
#cloud.aws.credentials.secretKey="secret"
1218

1319
# In AWS use this instead, and create an EC2 role
14-
#instanceProfile=true
20+
instanceProfile=false
1521

1622
# RDS access
1723
cloud.aws.rds.dbInstanceIdentifier=springaws

0 commit comments

Comments
 (0)