diff --git a/.classpath b/.classpath index 6d7587a..39abf1c 100644 --- a/.classpath +++ b/.classpath @@ -15,6 +15,7 @@ + @@ -27,5 +28,22 @@ + + + + + + + + + + + + + + + + + diff --git a/.project b/.project index cde6f78..10f966d 100644 --- a/.project +++ b/.project @@ -20,6 +20,11 @@ + + org.springframework.ide.eclipse.boot.validation.springbootbuilder + + + org.eclipse.m2e.core.maven2Builder diff --git a/README.md b/README.md index 69d6d96..305cddd 100644 --- a/README.md +++ b/README.md @@ -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. @@ -58,8 +48,59 @@ 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 spring-cloud-aws-sample-0.1.0.jar ubuntu@:/home/ubuntu/ +``` + ## Run the application +### Locally + For the impatient... git clone https://github.com/codeurjc/spring-cloud-aws-sample @@ -67,6 +108,12 @@ For the impatient... 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= --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" - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 9c9c475..a5a2257 100644 --- a/pom.xml +++ b/pom.xml @@ -5,13 +5,13 @@ es.urjc.code spring-cloud-aws-sample - 0.0.1-SNAPSHOT + 0.1.0-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent - 1.4.3.RELEASE + 2.1.2.RELEASE @@ -37,7 +37,7 @@ org.springframework.boot spring-boot-starter-data-jpa - + org.springframework.cloud spring-cloud-aws-autoconfigure @@ -66,7 +66,7 @@ org.springframework.cloud spring-cloud-dependencies - Camden.SR3 + Greenwich.RELEASE pom import diff --git a/src/main/java/es/urjc/code/daw/tablonanuncios/TablonController.java b/src/main/java/es/urjc/code/daw/tablonanuncios/TablonController.java index de87587..cc998c3 100644 --- a/src/main/java/es/urjc/code/daw/tablonanuncios/TablonController.java +++ b/src/main/java/es/urjc/code/daw/tablonanuncios/TablonController.java @@ -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 { @@ -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) { @@ -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); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ea61113..853859b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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