Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Apigee Proxy integration with AWS SQS

mehul3
New Member

Hello,

We are using AWS API Gateway and recently replaced AWS API Gateway with Google Cloud Apigee. We had AWS API Gateway integration with AWS SQS so that any message sent on REST endpoint will get directly stored in AWS SQS. In order to implement the same functionality using Apigee, I am using Java callout. Below is the sample code.

 

 

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import com.apigee.flow.execution.ExecutionContext;
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;

public class SendToSQS implements Execution {

    @Override
    public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {
        try {
            // Retrieve AWS credentials and SQS Queue URL from KVM
            String awsAccessKey = messageContext.getVariable("aws_access_key");
            String awsSecretKey = messageContext.getVariable("aws_secret_key");
            String awsRegion = messageContext.getVariable("aws_region");
            String queueUrl = messageContext.getVariable("queue_url");

            if (awsAccessKey == null || awsSecretKey == null || awsRegion == null || queueUrl == null) {
                throw new IllegalArgumentException("AWS credentials or SQS URL are not set in the KVM.");
            }

            String messageBody = messageContext.getMessage().getContent();

            // Initialize SQS Client with retrieved credentials and URL
            AwsBasicCredentials awsCreds = AwsBasicCredentials.create(awsAccessKey, awsSecretKey);
            SqsClient sqsClient = SqsClient.builder()
                    .region(Region.of(awsRegion))
                    .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                    .build();

            // Send message to SQS
            SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
                    .queueUrl(queueUrl)
                    .messageBody(messageBody)
                    .delaySeconds(5)
                    .build();
            sqsClient.sendMessage(sendMsgRequest);

            return ExecutionResult.SUCCESS;

        } catch (IllegalArgumentException e) {
            messageContext.getMessage().setContent("Configuration Error: " + e.getMessage());
            return ExecutionResult.ABORT;

        } catch (Exception e) {
            messageContext.getMessage().setContent("Execution Error: " + e.getMessage());
            return ExecutionResult.ABORT;
        }
    }
}

 

 

The build.gradle file is as follows.

 

 

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    // AWS SDK v2 for Java
    implementation 'software.amazon.awssdk:sqs:2.17.102'
    implementation 'com.apigee.edge:expressions:1.0.0'
    implementation 'com.apigee.edge:message-flow:1.0.0'
}

 

 

The issue is with Apigee dependencies not available in  a public domain. How do I get my code compiled? Do I need enterprise Apigee account to download jar locally ?

Thanks,

Mehul

 

 

0 1 304
1 REPLY 1

I agree with you, it's not very good that the Apigee jars are not available in some formal repo. Our documentation recommends that you manually download.

I use maven, not gradle. And this is what I use to get the jars into a local repo.

#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-

echo
echo "This script downloads JAR files and installs them into the local Maven repo."
echo

curl -O https://raw.githubusercontent.com/apigee/api-platform-samples/master/doc-samples/java-cookbook/lib/expressions-1.0.0.jar

 mvn install:install-file \
  -Dfile=expressions-1.0.0.jar \
  -DgroupId=com.apigee.edge \
  -DartifactId=expressions \
  -Dversion=1.0.0 \
  -Dpackaging=jar \
  -DgeneratePom=true

rm expressions-1.0.0.jar 

curl -O https://raw.githubusercontent.com/apigee/api-platform-samples/master/doc-samples/java-cookbook/lib/message-flow-1.0.0.jar

 mvn install:install-file \
  -Dfile=message-flow-1.0.0.jar \
  -DgroupId=com.apigee.edge \
  -DartifactId=message-flow \
  -Dversion=1.0.0 \
  -Dpackaging=jar \
  -DgeneratePom=true

rm message-flow-1.0.0.jar 

echo
echo done.
echo