Search code examples
javascripttypescriptaureliaamazon-sqsaws-sdk-js

How to use AWS SDK locally in my Aurelia Project


I want to use SQS Queue from AWS-SDK locally in my Aurelia project but I am facing problem.

If i use live URL of Amazon AWS-SDK link in Index.ejs then its working fine but if give local script path link then facing errors.

I tried many techniques but failed .In Live Link working fine and no error but in Local path "Config" is undefined error is getting.

Live PATH

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.505.0.min.js"></script>

Local Path

 <script src="./src/assets/aws-sdk-2.505.0.min.js"></script> 

on TS file I declare this

const AWS = (window as any).AWS;

  AWS.config.credentials = {
      "accessKeyId": "xxxxxxx",
      "secretAccessKey": "xxxxxx",
      "region": "xxxx"
    };
    AWS.config.region = "xxxxxx";
    AWS.config.version = "xxxxx";
    this.queueURL = "xxxxxxx";
 }

I can achieve this by NPM install but this is increasing size of project thats why I want to use locally in project


Solution

  • If size of the bundle is the main issue for you, aws-sdk provides the option to include only the services you need. There is a bit of explanation on this in the aws-sdk README where you can do the following with the npm package imported:

    // option 1. import entire SDK
    // this is big, indeed
    import * as aws from 'aws-sdk';
    
    // option 2a. import AWS object without services
    // that's pretty basic
    import * as aws from 'aws-sdk/global';
    
    // option 2b. import individual service
    // this might be just what you need, combined with option 2a.
    import * as s3 from 'aws-sdk/clients/s3';
    

    Important notice: I changed the import a bit than what the README states. Simply because the TS compiles errors out that the aws-sdk module has no default export.

    A full sample, based on your original example, that would significantly reduce the size of your output bundle would be:

    // only include the 'global' (core) and 's3' service
    import * as aws from 'aws-sdk/global';
    import * as s3 from 'aws-sdk/clients/s3';
    
    export class App {
      public attached() {
        aws.config.credentials = {
          "accessKeyId": "xxxxxxx",
          "secretAccessKey": "xxxxxx"
        };
    
        aws.config.region = "xxxxxx";
        aws.config.apiVersion = "xxxxx";
      }
    }
    

    Not to be nitpicky, but some of the options from your sample simply don't work - so these are removed.

    Still, if that isn't enough convincing, you could try reverting to including the CDN in the <head> tag of index.ejs and declare it globally using var on top of your class:

    var AWS = require('aws-sdk');
    

    ... but I wouldn't go that way and would personally favour a larger bundle over an additional remote http call.