Created
January 24, 2022 04:02
-
-
Save sathishjayapal/8e5aecfd412d1212a5460bb32c0a2df1 to your computer and use it in GitHub Desktop.
aws profile cdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.sathish.aws.common; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import software.amazon.awscdk.core.Tags; | |
import software.amazon.awscdk.services.s3.Bucket; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.time.LocalDateTime; | |
import java.util.Properties; | |
public interface CSVFileParserCDKInterface { | |
Logger LOGGER = LoggerFactory.getLogger("CSVFileParserCDKInterface"); | |
String fileName = "config.properties"; | |
ThreadLocal<String> PROJ_ENV = ThreadLocal.withInitial(() -> "project.env"); | |
String PROJ_NAME = "project.name"; | |
String PROJ_TECH = "project.technology"; | |
String PROJECT_FUNCTIONAL = "project.functional"; | |
ThreadLocal<Properties> properties = ThreadLocal.withInitial(() -> { | |
try { | |
return loadAndReturnProps(); | |
} catch (Exception e) { | |
return null; | |
} | |
}); | |
static String makeCDKIdentifier() throws Exception { | |
return new StringBuilder() | |
.append(properties.get().getProperty(PROJ_NAME)) | |
.append("-").append(properties.get().getProperty(PROJ_ENV.get())).append("-").append(properties.get() | |
.get(PROJ_TECH)).append("-").append(properties.get().get(PROJECT_FUNCTIONAL)).toString(); | |
} | |
static Properties loadAndReturnProps() throws Exception { | |
Properties prop = null; | |
try (InputStream input = CSVFileParserCDKInterface.class.getClassLoader().getResourceAsStream(fileName)) { | |
prop = new Properties(); | |
if (input == null) { | |
LOGGER.error("Properties file not loaded"); | |
throw new Exception("Properties file not loaded"); | |
} | |
prop.load(input); | |
} catch (IOException ex) { | |
LOGGER.error("Error for props file", ex.fillInStackTrace()); | |
} | |
return prop; | |
} | |
default void createTagsForCsvBucket(Bucket createdBucket) { | |
Tags.of(createdBucket).add("createdate", LocalDateTime.now().toString()); | |
Tags.of(createdBucket).add("expiredate", LocalDateTime.now().plusDays(60).toString()); | |
} | |
default String makeCDKIdentifier(String SERVICE_TYPE) throws Exception { | |
return new StringBuilder().append(CSVFileParserCDKInterface.makeCDKIdentifier()).append("-").append(SERVICE_TYPE).toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.sathish; | |
import lombok.extern.slf4j.Slf4j; | |
import me.sathish.aws.create.s3.S3BucketCreate; | |
import me.sathish.aws.create.vpc.VPCCreate; | |
import software.amazon.awscdk.core.App; | |
import software.amazon.awscdk.core.Environment; | |
import software.amazon.awscdk.core.StackProps; | |
@Slf4j | |
public final class MainResourceCreator { | |
static Environment makeEnv(String account, String region) { | |
account = (account == null) ? System.getenv("CDK_DEFAULT_ACCOUNT") : account; | |
region = (region == null) ? System.getenv("CDK_DEFAULT_REGION") : region; | |
System.out.println("THe account us " + account); | |
return Environment.builder() | |
.account(account) | |
.region(region) | |
.build(); | |
} | |
public static void main(final String[] args) throws Exception { | |
App app = new App(); | |
Environment envEU = makeEnv(null, null); | |
S3BucketCreate s3Stack = new S3BucketCreate(app, "S3BucketCreate", StackProps.builder() | |
.env(envEU).build()); | |
s3Stack.getNotificationArns().forEach(data -> System.out.println("data " + data)); | |
// VPCCreate vpcCreate = new VPCCreate(app, "VPCCreate"); | |
// vpcCreate.getNotificationArns().forEach(data -> System.out.println("data " + data)); | |
app.synth(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.sathish.aws.create.s3; | |
import me.sathish.aws.common.CSVFileParserCDKInterface; | |
import software.amazon.awscdk.core.*; | |
import software.amazon.awscdk.services.s3.BlockPublicAccess; | |
import software.amazon.awscdk.services.s3.Bucket; | |
import software.amazon.awscdk.services.s3.BucketAccessControl; | |
import java.time.LocalDateTime; | |
import static software.amazon.awscdk.services.s3.Bucket.Builder.create; | |
public class S3BucketCreate extends Stack implements CSVFileParserCDKInterface { | |
static final String SERVICE_TYPE = "bucket"; | |
public S3BucketCreate(final Construct parent, final String id) throws Exception { | |
this(parent, id, null); | |
} | |
public S3BucketCreate(final Construct parent, final String id, final StackProps props) throws Exception { | |
super(parent, id, props); | |
String s3identifierInformation = makeCDKIdentifier(SERVICE_TYPE); | |
Bucket.Builder mainBucketBuilder = create(this, s3identifierInformation).versioned(true).removalPolicy(RemovalPolicy.DESTROY).autoDeleteObjects(true); | |
mainBucketBuilder.bucketName(s3identifierInformation); | |
mainBucketBuilder.accessControl(BucketAccessControl.PRIVATE); | |
mainBucketBuilder.blockPublicAccess(BlockPublicAccess.BLOCK_ALL); | |
mainBucketBuilder.enforceSsl(Boolean.TRUE); | |
Bucket createdBucket = mainBucketBuilder.build(); | |
createTagsForCsvBucket | |
(createdBucket); | |
System.out.println("The created bucket is " + createdBucket.getBucketName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment