AWS and Salesforce Integration using AWS Signature Version 1
Hi! 🙂
I was recently working on an integration between Salesforce and AWS, and along the way there were times where I needed help but I couldn’t find much help online. Eventually I was able to do it, *phew*, and thought to blog about since there aren’t much resources to look for.
So in this article I will be sharing a code snippet of how you can get a list of all the EC2 images from AWS.
Pre-requisites
Before you start writing the code, you will need an AWS account with a Connected app created, to get the Access Key and the Secret Key.
Code
AWSCallout.apxc class
public class AWSCallout { private String AccessKey='XXXXXXXXXXXXX'; //Replace with your AWS Connected app's Access Key private String SecretKey='XXXXXXXXXXXXX'; //Replace with your AWS Connected app's Secret Key private String ec2version = '2016-11-15'; public void DescribeImages(String owner) { //this needs to be the same as the querystring but without the '?' '=' and '&', no urlencoding and not the signature itself String tosign = 'ActionDescribeImagesAWSAccessKeyId'+AccessKey+'Owner.1'+owner+'SignatureVersion1Timestamp'+TimeStamp()+'Version'+ec2version; String signature = make_sig(tosign); String querystring = '?Action=DescribeImages&AWSAccessKeyId='+AccessKey+'&Owner.1='+owner+'&SignatureVersion=1&Timestamp='+EncodingUtil.urlEncode(TimeStamp(), 'UTF-8')+'&Version='+ec2version+'&Signature='+EncodingUtil.urlEncode(signature, 'UTF-8'); HttpResponse res = makerequest(querystring); system.debug(res.getBody()); } //make the request to Amazon EC2, this just wraps the Http communication public HttpResponse makerequest(String querystring) { HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setCompressed(true); req.setEndpoint('https://ec2.amazonaws.com/'+querystring); Http http = new Http(); HttpResponse res = http.send(req); system.debug(res.getStatus()); system.debug(res.getStatusCode()); system.debug(res.getBody()); return res; } private String TimeStamp() { Datetime now = Datetime.now(); return now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'Z'; } /* Function to create the required signature for the HTTP Request*/ private String make_sig(string canonicalBuffer) { String macUrl ; String signingKey = EncodingUtil.base64Encode(Blob.valueOf(SecretKey)); Blob mac = Crypto.generateMac('HMacSHA1', blob.valueof(canonicalBuffer),blob.valueof(SecretKey)); macUrl = EncodingUtil.base64Encode(mac); return macUrl; }}
After creating the Apex class, you can simply call the DescribeImages() in the anonymous window method to get the EC2 images list.
Like this:
string owner = '368772107083'; //Replace with your AWS owner's Id.AWSCallout obj = new AWSCallout();obj.DescribeImages(owner);
And voila! 😀
If you liked this article, then am sure you will love this one: https://forcepanda.wordpress.com/2019/11/19/salesforce-and-aws-integration-made-easy-via-named-credentials/
Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

.png)




Comments