Using AWS-CLI with Angular

I’ve just spent a frustrating 3 days trying to figure out how to use AWS-CLI with Angular.

I love using AWS. But I do find their documentation a scavenger hunt: I can’t find the answers I’m looking for in one or two documents. I end up searching dozens to find the 2 or 3 answers I need to proceed.

So, my scenario: I wrote some AWS Lambda functions that return data I need for a website. I first did the common thing: write the functions, then attach them to API Gateways. The problem: the Gateways keep giving me 500 errors and I can’t seem to find a solution.

So I decided to switch to using the aws-cli library in Angular. Not an easy feat.

Mind you, I love AWS. I love the toolset and the options it allows you. But the documentation sucks big time! There is no easy way to complete a task. You end up searching a dozen online documents to find the answers you need to accomplish a simple task. In my case, that meant that I wanted to call Lambda, from an application that was being deployed to Heroku. That meant that I couldn’t depend on the .aws/credentials file; I would need to enter credentials in my code. This was the first hurdle. The API implied that this would be possible, as did the tutorial I found. I finally worked it out:

this.lambda = new AWS.Lambda({
      credentials: {
        accessKeyId: credentials['accessKeyId'],
        secretAccessKey: credentials['secretAccessKey']
      }
    });
    this.lambda.config.update({
      region: credentials['region']
    });

And no, the region can’t be added with the credentials. It didn’t work.

The next ‘oopsie’ I ran into was the endpoint. The service kept calling https://lambda.null.amazonaws.com. You would think it would be smart enough to figure out what the region the endpoint was in when you set the region. I guess not. So I had to set that as well:

this.lambda.endpoint = new Endpoint(credentials['lambdaEndpoint']);

After that, I had issues setting up the parameters. For some reason, my slow brain had problems with the Payload on this. I had this weird JSON error: Unexpected token o in JSON. Luckily Stackoverflow offered me a solution. It has to do with the fact you’re trying to turn a JSON object into a JSON object. So my final solution looked like this:

var payload = { "proxy": "labels" };
      var params = {
        FunctionName: credentials['lambdaCurrentIssues'],
        InvocationType: 'RequestResponse',
        LogType: 'Tail',
        Payload: JSON.stringify(payload)
      }

No, setting Payload to payload didn’t work. But this did.

Oh, as for the LogType, I’d skip that and stick to reading your CloudWatch logs. The ‘Tail’ information is encoded. I didn’t spend the time to figure out how to decode it to read it.

And that did it! I wrapped my functions in Promises to handle the delays. And delays there were! My implementation takes a minute to load the page, which, obviously, I will need to handle at some point. But for now, I have the website working.