Instance profile credentials example. (#3401)

* Instance profile credentials example.

* InstanceProfile CloudFormation template included to provision AWS
environment.

* Included java package installation under Yaml script.

* Using SpringApplication reference to prepare application context.

* Introducing SpringCloudS3Service to handle all S3 operations.
This commit is contained in:
Satish Pandey 2018-01-27 10:54:22 +05:30 committed by Grzegorz Piwowarek
parent 5d6c47789e
commit fab4aec7a1
6 changed files with 222 additions and 0 deletions

View File

@ -19,3 +19,8 @@ to write the following in `application.properties`:
cloud.aws.rds.spring-cloud-test-db
cloud.aws.rds.spring-cloud-test-db.password=se3retpass
```
Multiple application classes are available under this project. To launch InstanceProfileAwsApplication application, replace `start-class` under `pom.xml`:
```
<start-class>com.baeldung.spring.cloud.aws.InstanceProfileAwsApplication</start-class>
```

View File

@ -19,6 +19,7 @@
</parent>
<properties>
<start-class>com.baeldung.spring.cloud.aws.SpringCloudAwsApplication</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

View File

@ -0,0 +1,60 @@
package com.baeldung.spring.cloud.aws;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.baeldung.spring.cloud.aws.s3.SpringCloudS3Service;
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.baeldung.spring.cloud.aws.s3")
public class InstanceProfileAwsApplication {
private static final Logger logger = LoggerFactory.getLogger(InstanceProfileAwsApplication.class);
private static final String applicationConfig = "spring.config.name:application-instance-profile";
private static String bucketName;
private static String fileName = "sample-file.txt";
private static void setupResources() {
bucketName = "baeldung-test-" + UUID.randomUUID()
.toString();
try {
Files.write(Paths.get(fileName), "Hello World!".getBytes());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
public static void main(String[] args) {
setupResources();
if (!new File(fileName).exists()) {
logger.warn("Not able to create {} file. Check your folder permissions.", fileName);
System.exit(1);
}
SpringApplication application = new SpringApplicationBuilder(InstanceProfileAwsApplication.class).properties(applicationConfig)
.build();
ConfigurableApplicationContext context = application.run(args);
SpringCloudS3Service service = context.getBean(SpringCloudS3Service.class);
// S3 bucket operations
service.createBucket(bucketName);
service.uploadObject(bucketName, fileName);
service.downloadObject(bucketName, fileName);
service.deleteBucket(bucketName);
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.spring.cloud.aws.s3;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
@Component
public class SpringCloudS3Service {
private static final Logger logger = LoggerFactory.getLogger(SpringCloudS3Service.class);
@Autowired
AmazonS3 amazonS3;
@Autowired
SpringCloudS3 springCloudS3;
public void createBucket(String bucketName) {
logger.debug("Creating S3 bucket: {}", bucketName);
amazonS3.createBucket(bucketName);
logger.info("{} bucket created successfully", bucketName);
}
public void downloadObject(String bucketName, String objectName) {
String s3Url = "s3://" + bucketName + "/" + objectName;
try {
springCloudS3.downloadS3Object(s3Url);
logger.info("{} file download result: {}", objectName, new File(objectName).exists());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
public void uploadObject(String bucketName, String objectName) {
String s3Url = "s3://" + bucketName + "/" + objectName;
File file = new File(objectName);
try {
springCloudS3.uploadFileToS3(file, s3Url);
logger.info("{} file uploaded to S3", objectName);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
public void deleteBucket(String bucketName) {
logger.trace("Deleting S3 objects under {} bucket...", bucketName);
ListObjectsV2Result listObjectsV2Result = amazonS3.listObjectsV2(bucketName);
for (S3ObjectSummary objectSummary : listObjectsV2Result.getObjectSummaries()) {
logger.info("Deleting S3 object: {}", objectSummary.getKey());
amazonS3.deleteObject(bucketName, objectSummary.getKey());
}
logger.info("Deleting S3 bucket: {}", bucketName);
amazonS3.deleteBucket(bucketName);
}
}

View File

@ -0,0 +1,78 @@
AWSTemplateFormatVersion: 2010-09-09
Metadata:
'AWS::CloudFormation::Designer':
157e7d5f-5cb3-4a23-a50c-97e7f6c57173:
size:
width: 60
height: 60
position:
x: 450
'y': 90
z: 0
embeds: []
9bbaaa55-9cba-4555-a7c6-fb6ac248fd3a:
size:
width: 60
height: 60
position:
x: 260
'y': 90
z: 0
embeds: []
isassociatedwith:
- 157e7d5f-5cb3-4a23-a50c-97e7f6c57173
a7348729-a594-4dca-9b0a-e1c8d777dc3b:
size:
width: 60
height: 60
position:
x: 70
'y': 90
z: 0
embeds: []
Resources:
IAMRoleBaeldung:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AmazonS3FullAccess'
Metadata:
'AWS::CloudFormation::Designer':
id: 157e7d5f-5cb3-4a23-a50c-97e7f6c57173
InstanceProfileBaeldung:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Roles:
- !Ref IAMRoleBaeldung
Metadata:
'AWS::CloudFormation::Designer':
id: 9bbaaa55-9cba-4555-a7c6-fb6ac248fd3a
EC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: ami-2581aa40
InstanceType: t2.micro
IamInstanceProfile: !Ref InstanceProfileBaeldung
KeyName: Satish-Ohio
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash
- |
apt -y install openjdk-8-jre-headless
Metadata:
'AWS::CloudFormation::Designer':
id: a7348729-a594-4dca-9b0a-e1c8d777dc3b
DependsOn:
- InstanceProfileBaeldung

View File

@ -0,0 +1,14 @@
# Don't try to create DataSouce when running tests which don't need a DataSource
spring.autoconfigure.exclude=\
org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
cloud.aws.region.auto=true
# Load instance profile credentials
cloud.aws.credentials.instanceProfile=true
# Disable auto cloud formation
cloud.aws.stack.auto=false
# Disable web environment
spring.main.web-environment=false