removed cdk code
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
{
|
||||
"app": "mvn -e -q compile exec:java",
|
||||
"watch": {
|
||||
"include": [
|
||||
"**"
|
||||
],
|
||||
"exclude": [
|
||||
"README.md",
|
||||
"cdk*.json",
|
||||
"target",
|
||||
"pom.xml",
|
||||
"src/test"
|
||||
]
|
||||
},
|
||||
"context": {
|
||||
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
|
||||
"@aws-cdk/core:stackRelativeExports": true,
|
||||
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
|
||||
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
|
||||
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
|
||||
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
|
||||
"@aws-cdk/core:target-partitions": [
|
||||
"aws",
|
||||
"aws-cn"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,13 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.myorg</groupId>
|
||||
<artifactId>lambda-from-sqs</artifactId>
|
||||
<version>0.1</version>
|
||||
<groupId>io.pratik</groupId>
|
||||
<artifactId>sqs</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<cdk.version>2.8.0</cdk.version>
|
||||
<constructs.version>[10.0.0,11.0.0)</constructs.version>
|
||||
<junit.version>5.7.1</junit.version>
|
||||
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
@@ -28,30 +26,11 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<mainClass>com.myorg.LambdaFromSqsApp</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- AWS Cloud Development Kit -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awscdk</groupId>
|
||||
<artifactId>aws-cdk-lib</artifactId>
|
||||
<version>${cdk.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>software.constructs</groupId>
|
||||
<artifactId>constructs</artifactId>
|
||||
<version>${constructs.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
@@ -63,12 +42,7 @@
|
||||
<artifactId>sns</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.myorg;
|
||||
|
||||
import software.amazon.awscdk.App;
|
||||
import software.amazon.awscdk.Environment;
|
||||
import software.amazon.awscdk.StackProps;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LambdaFromSqsApp {
|
||||
public static void main(final String[] args) {
|
||||
App app = new App();
|
||||
|
||||
new LambdaFromSqsStack(app, "LambdaFromSqsStack", StackProps.builder()
|
||||
// If you don't specify 'env', this stack will be environment-agnostic.
|
||||
// Account/Region-dependent features and context lookups will not work,
|
||||
// but a single synthesized template can be deployed anywhere.
|
||||
|
||||
// Uncomment the next block to specialize this stack for the AWS Account
|
||||
// and Region that are implied by the current CLI configuration.
|
||||
/*
|
||||
.env(Environment.builder()
|
||||
.account(System.getenv("CDK_DEFAULT_ACCOUNT"))
|
||||
.region(System.getenv("CDK_DEFAULT_REGION"))
|
||||
.build())
|
||||
*/
|
||||
|
||||
// Uncomment the next block if you know exactly what Account and Region you
|
||||
// want to deploy the stack to.
|
||||
/*
|
||||
.env(Environment.builder()
|
||||
.account("123456789012")
|
||||
.region("us-east-1")
|
||||
.build())
|
||||
*/
|
||||
|
||||
// For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
|
||||
.build());
|
||||
|
||||
app.synth();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.myorg;
|
||||
|
||||
import software.amazon.awscdk.Duration;
|
||||
import software.amazon.awscdk.Stack;
|
||||
import software.amazon.awscdk.StackProps;
|
||||
import software.amazon.awscdk.services.lambda.AssetCode;
|
||||
import software.amazon.awscdk.services.lambda.Function;
|
||||
import software.amazon.awscdk.services.lambda.IEventSource;
|
||||
import software.amazon.awscdk.services.lambda.Runtime;
|
||||
import software.amazon.awscdk.services.lambda.eventsources.SqsEventSource;
|
||||
import software.amazon.awscdk.services.sns.Topic;
|
||||
import software.amazon.awscdk.services.sns.subscriptions.SqsSubscription;
|
||||
import software.amazon.awscdk.services.sqs.DeadLetterQueue;
|
||||
import software.amazon.awscdk.services.sqs.DeduplicationScope;
|
||||
import software.amazon.awscdk.services.sqs.Queue;
|
||||
import software.constructs.Construct;
|
||||
|
||||
public class LambdaFromSqsStack extends Stack {
|
||||
public LambdaFromSqsStack(final Construct scope, final String id) {
|
||||
this(scope, id, null);
|
||||
}
|
||||
|
||||
public LambdaFromSqsStack(final Construct scope, final String id, final StackProps props) {
|
||||
super(scope, id, props);
|
||||
|
||||
Queue dlq = Queue.Builder.create(this, "mydlq")
|
||||
.queueName("mydlq")
|
||||
.build();
|
||||
|
||||
DeadLetterQueue deadLetterQueue = DeadLetterQueue.builder()
|
||||
.queue(dlq)
|
||||
.maxReceiveCount(10)
|
||||
.build();
|
||||
|
||||
Queue queue = Queue.Builder.create(this, "myqueue")
|
||||
.queueName("myqueue")
|
||||
.deadLetterQueue(deadLetterQueue )
|
||||
.visibilityTimeout(Duration.minutes(5))
|
||||
.deliveryDelay(Duration.minutes(1))
|
||||
.maxMessageSizeBytes(1024*100) // 100 Kb
|
||||
.receiveMessageWaitTime(Duration.seconds(5))
|
||||
.retentionPeriod(Duration.days(1))
|
||||
.build();
|
||||
|
||||
Queue fifoQueue = Queue.Builder.create(this, "myfifoqueue")
|
||||
.queueName("myfifoqueue.fifo")
|
||||
.deduplicationScope(DeduplicationScope.MESSAGE_GROUP)
|
||||
.contentBasedDeduplication(false)
|
||||
.fifo(true).build();
|
||||
|
||||
|
||||
|
||||
Function sqsReceiver = Function.Builder.create(this, "SQSReceiver")
|
||||
.code(AssetCode.fromAsset("resources")) .handler("index.handler") .runtime(
|
||||
Runtime.NODEJS_14_X) .build();
|
||||
|
||||
IEventSource source =
|
||||
SqsEventSource.Builder.create(queue).batchSize(5).build();
|
||||
sqsReceiver.addEventSource(source);
|
||||
|
||||
Topic topic = Topic.Builder.create(this, "mytopic").build();
|
||||
|
||||
|
||||
topic.addSubscription(
|
||||
SqsSubscription.Builder.create(queue).build());
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,9 @@ public class MessageSender {
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
sendMessage();
|
||||
// sendMessage();
|
||||
// sendMessageToFifo();
|
||||
// sendMessageToSnsTopic();
|
||||
sendMessageToSnsTopic();
|
||||
}
|
||||
public static void sendMessageToSnsTopic() {
|
||||
SnsClient snsClient = getSNSClient();
|
||||
@@ -45,10 +45,10 @@ public class MessageSender {
|
||||
String traceId = UUID.randomUUID().toString();
|
||||
// add traceId as a message attribute
|
||||
messageAttributes.put(TRACE_ID_NAME, MessageAttributeValue.builder().dataType("String").stringValue(traceId).build());
|
||||
|
||||
final String topicArn = "arn:aws:sns:us-east-1:" +AppConfig.ACCOUNT_NO + ":LambdaFromSqsStack-mytopicDA9518A7-18LSXENGTNKKY";
|
||||
final String topicArn = "arn:aws:sns:us-east-1:" + AppConfig.ACCOUNT_NO + ":mytopic";
|
||||
|
||||
PublishRequest publishRequest = PublishRequest.builder().topicArn(topicArn).message("Test message published to topic").build();
|
||||
PublishResponse publishResponse = snsClient.publish(publishRequest );//sendMessage(sendMessageRequest);
|
||||
PublishResponse publishResponse = snsClient.publish(publishRequest);
|
||||
|
||||
logger.info("message id: "+ publishResponse.messageId());
|
||||
|
||||
|
||||
138
aws/sqs/src/main/java/io/pratik/sqs/ResourceHelper.java
Normal file
138
aws/sqs/src/main/java/io/pratik/sqs/ResourceHelper.java
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package io.pratik.sqs;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
||||
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.sns.SnsClient;
|
||||
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
|
||||
import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
|
||||
import software.amazon.awssdk.services.sns.model.SubscribeRequest;
|
||||
import software.amazon.awssdk.services.sns.model.SubscribeResponse;
|
||||
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
|
||||
import software.amazon.awssdk.services.sqs.model.CreateQueueResponse;
|
||||
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
|
||||
|
||||
/**
|
||||
* @author pratikdas
|
||||
*
|
||||
*/
|
||||
public class ResourceHelper {
|
||||
private static Logger logger = Logger.getLogger(ResourceHelper.class.getName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
// createStandardQueue();
|
||||
// createFifoQueue();
|
||||
createSNSTopicWithSubscription();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void createStandardQueue() {
|
||||
SqsClient sqsClient = getSQSClient();
|
||||
|
||||
String dlqName = "mydlq";
|
||||
CreateQueueRequest createQueueRequest = CreateQueueRequest.builder()
|
||||
.queueName(dlqName)
|
||||
.build();
|
||||
|
||||
|
||||
// Create dead letter queue
|
||||
CreateQueueResponse createQueueResponse = sqsClient.createQueue(createQueueRequest);
|
||||
|
||||
|
||||
String dlqArn = getQueueArn(dlqName,"us-east-1");
|
||||
|
||||
Map<QueueAttributeName, String> attributeMap = new HashMap<QueueAttributeName, String>();
|
||||
attributeMap.put(QueueAttributeName.REDRIVE_POLICY,
|
||||
"{\"maxReceiveCount\":10,\"deadLetterTargetArn\":\""+dlqArn+"\"}");
|
||||
|
||||
// Prepare request for creating the standard queue
|
||||
createQueueRequest = CreateQueueRequest.builder()
|
||||
.queueName("myqueue")
|
||||
.attributes(attributeMap)
|
||||
.build();
|
||||
|
||||
// create the queue
|
||||
createQueueResponse = sqsClient.createQueue(createQueueRequest);
|
||||
|
||||
logger.info("Queue URL " + createQueueResponse.queueUrl());
|
||||
}
|
||||
|
||||
public static void createFifoQueue() {
|
||||
SqsClient sqsClient = getSQSClient();
|
||||
|
||||
|
||||
Map<QueueAttributeName, String> attributeMap = new HashMap<QueueAttributeName, String>();
|
||||
|
||||
attributeMap.put(QueueAttributeName.FIFO_QUEUE, "true");
|
||||
attributeMap.put(QueueAttributeName.DEDUPLICATION_SCOPE, "messageGroup");
|
||||
attributeMap.put(QueueAttributeName.CONTENT_BASED_DEDUPLICATION, "false");
|
||||
|
||||
CreateQueueRequest createQueueRequest = CreateQueueRequest.builder()
|
||||
.queueName("myfifoqueue.fifo")
|
||||
.attributes(attributeMap )
|
||||
.build();
|
||||
|
||||
CreateQueueResponse createQueueResponse = sqsClient.createQueue(createQueueRequest);
|
||||
logger.info("url "+createQueueResponse.queueUrl());
|
||||
}
|
||||
|
||||
public static void createSNSTopicWithSubscription() {
|
||||
SnsClient snsClient = getSNSClient();
|
||||
|
||||
CreateTopicRequest createTopicRequest = CreateTopicRequest.builder().name("mytopic").build();
|
||||
CreateTopicResponse createTopicResponse = snsClient.createTopic(createTopicRequest );
|
||||
|
||||
String topicArn = createTopicResponse.topicArn();
|
||||
//Topic topic = Topic.builder().topicArn(topicArn).build();
|
||||
|
||||
String queueArn= getQueueArn("myqueue","us-east-1");
|
||||
|
||||
SubscribeRequest subscribeRequest = SubscribeRequest.builder()
|
||||
.protocol("sqs")
|
||||
.topicArn(topicArn)
|
||||
.endpoint(queueArn)
|
||||
.build();
|
||||
SubscribeResponse subscribeResponse = snsClient.subscribe(subscribeRequest );
|
||||
|
||||
|
||||
logger.info("subscriptionArn " + subscribeResponse.subscriptionArn());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static SqsClient getSQSClient() {
|
||||
AwsCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create("pratikpoc");
|
||||
|
||||
SqsClient sqsClient = SqsClient
|
||||
.builder()
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.region(Region.US_EAST_1).build();
|
||||
return sqsClient;
|
||||
}
|
||||
|
||||
private static SnsClient getSNSClient() {
|
||||
AwsCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create("pratikpoc");
|
||||
|
||||
SnsClient snsClient = SnsClient
|
||||
.builder()
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.region(Region.US_EAST_1).build();
|
||||
return snsClient;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String getQueueArn(final String queueName, final String region) {
|
||||
return "arn:aws:sqs:"+region + ":" + AppConfig.ACCOUNT_NO+ ":" + queueName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
// package com.myorg;
|
||||
|
||||
// import software.amazon.awscdk.App;
|
||||
// import software.amazon.awscdk.assertions.Template;
|
||||
// import java.io.IOException;
|
||||
|
||||
// import java.util.HashMap;
|
||||
|
||||
// import org.junit.jupiter.api.Test;
|
||||
|
||||
// example test. To run these tests, uncomment this file, along with the
|
||||
// example resource in java/src/main/java/com/myorg/LambdaFromSqsStack.java
|
||||
// public class LambdaFromSqsTest {
|
||||
|
||||
// @Test
|
||||
// public void testStack() throws IOException {
|
||||
// App app = new App();
|
||||
// LambdaFromSqsStack stack = new LambdaFromSqsStack(app, "test");
|
||||
|
||||
// Template template = Template.fromStack(stack);
|
||||
|
||||
// template.hasResourceProperties("AWS::SQS::Queue", new HashMap<String, Number>() {{
|
||||
// put("VisibilityTimeout", 300);
|
||||
// }});
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user