Wednesday 7 June 2017

How to finish converting Frontend AWS Lambda Function into an Angular 2 Service

I have a front end AWS lambda function that I am running in the browser.

Here is the function as it currently is used in the browser:

$(document).ready(function(){
    $('#contact-form-button').on('click', function(e){
        AWS.config.update({region: 'us-east-1'});
        AWS.config.credentials = new AWS.Credentials('XXXXXXXXXXXX', '0000000000000');
        var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

        var pullParams = {
            FunctionName: 'marketing-web-dev-createFeedback',
            InvocationType: 'RequestResponse',
            LogType: 'Tail',
            Payload: JSON.stringify({
                "feedback_id": makeid(),
                "name": $('#name').val(),
                "email": $('#email').val(),
                "subject": $('#subject').val(),
                "message": $('#message').val()
            })
        };

        lambda.invoke(pullParams, function(error, data) {
            if (error) {
                prompt(error);
            } else {
                pullResults = JSON.parse(data.Payload);
            }
        });
        var pullResults;
        return false;
    });
});
function makeid(){
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

I want to use this same functionality in an Angular 2 Application and share it with my components as a service. I have gotten to the point where I am running the lambda.invoke method inside a new arrow function in my Angular 2 Service I am just confused on how to pass the payload parameters here are the components of my Angular 2 Service, first my aws.variables.ts

interface AWSConfig {
  accessKeyId: string;
  secretAccessKey: string;
  region: string;
  apiVersion: string;
}

export const AWS_CONFIG: AWSConfig= {
  accessKeyId: 'AKIAIPE54LWXYNXASZTQ',
  secretAccessKey: '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',
  region: 'us-east-1',
  apiVersion: '2015-03-31'
};

Now here is the actual service:

import { Injectable } from '@angular/core';
import {AWS_CONFIG} from './aws.variables';
import * as AWS from 'aws-sdk';

@Injectable()
export class LambdaService {
  functionName:string;
  invocationType: string;
  logType: string;
  payload: any;

  config = new AWS.Config({
    accessKeyId: AWS_CONFIG.accessKeyId, secretAccessKey: AWS_CONFIG.secretAccessKey
  });
  lambda = new AWS.Lambda({ region: AWS_CONFIG.region, apiVersion: AWS_CONFIG.apiVersion});

  invokeLambda(){
    this.lambda.invoke({
      FunctionName: this.functionName,
      InvocationType: this.invocationType,
        LogType: this.logType,
        Payload: this.payload
    }, (err, data)=>{
      if(err) {
        console.log(err);
      }else{

      }
    })
  }
  
  constructor() {  }

}

Can this service be implemented, I know I wrote it, but obviously if I am asking for help I need to know am I over or under engineering it? As you can see I don't have anything for the else function, that is where my road block is.



via Amen Ra

No comments:

Post a Comment