Code Monkey home page Code Monkey logo

amazon-api-gateway-url-shortener's Introduction

Functionless URL Shortener

This app creates a URL shortener without using any compute. All business logic is handled at the Amazon API Gateway level. The basic app will create an API Gateway instance utilizing Cognito for authentication and authorization. It will also create an Amazon DynamoDB table for data storage. It will also create a simple Vuejs application as a demo client.

Read the blog series about this application:

  1. Building a serverless URL shortener app without AWS Lambda – part 1
  2. Building a serverless URL shortener app without AWS Lambda – part 2
  3. Building a serverless URL shortener app without AWS Lambda – part 3

The Backend

Services Used

Requirements for deployment

  • AWS CLI

  • AWS SAM CLI v0.37.0+

  • Forked copy of this repository. Instructions for forking a GitHib repository can be found here

  • A GitHub personal access token with the repo scope as shown below. Instructions for creating a personal access token can be found here

    Personal access token scopes

    Be sure and store you new token in a place that you can find it.

Deploying

Note: This stack includes an Amazon CloudFront distribution which can take around 30 minutes to create. Don't be alarmed if the deploy seems to hang for a long time. In the terminal, use the SAM CLI guided deployment the first time you deploy

sam deploy -g

Choose options

You can choose the default for all options except GithubRepository and **

## The name of the CloudFormation stack
Stack Name [URLShortener]:

## The region you want to deploy in
AWS Region [us-west-2]:

## The name of the application (lowercase no spaces). This must be globally unique
Parameter AppName [shortener]:

## Enables public client and local client for testing. (Less secure)
Parameter UseLocalClient [false]:

## GitHub forked repository URL
Parameter GithubRepository []:

## Github Personal access token
Parameter PersonalAccessToken:

## Shows you resources changes to be deployed and requires a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: 

## SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]:

## Save your choice for later deployments
Save arguments to samconfig.toml [Y/n]:

SAM will then deploy the AWS CloudFormation stack to your AWS account and provide required outputs for the included client.

After the first deploy you may re-deploy using sam deploy or redeploy with different options using sam deploy -g.

The Client

The client can also be run locally for debugging. Instructions can be found here.

The client is a Vue.js application that interfaces with the backend and allows you to authenticate and manage URL links. The client is hosted using Amplify Console. To avoid circular dependencies, we need to provide some information for the client after stack is built. The information needed is provided at the end of the deploy process. If you do not have the information you can run the following:

aws cloudformation describe-stacks --stack-name URLShortener

We need to add this information to the environment variables for the Amplify Console app. There are two options for adding the variables.

Option 1: using the AWS CLI (Update the <values> to reflect the information returned from the deployment.)

aws amplify update-app --app-id <MyAmplifyAppId> --environment-variables \
VUE_APP_NAME=<VueAppName>\
,VUE_APP_CLIENT_ID=<VUE_APP_CLIENT_ID>\
,VUE_APP_API_ROOT=<VUE_APP_API_ROOT>\
,VUE_APP_AUTH_DOMAIN=<VUE_APP_AUTH_DOMAIN>

Also available in the stack output as AmplifyEnvironmentUpdateCommand

Option 2: Amplify Console page

  1. Open the Amplify Console page
  2. On the left side, under All apps, choose Url-Shortner-Client
  3. Under App settings choose Environment variables
  4. Choose the manage variables button
  5. Choose add variable
  6. Fill in the variable and it's corresponding Value
  7. Leave defaults for Branches and Actions
  8. Repeat for all four variables
  9. Choose save

Starting the first deployment

After deploying the CloudFormation template, you need to go into the Amplify Console and trigger a build. The CloudFormation template can provision the resources, but can’t trigger a build since it creates resources but cannot trigger actions. This can be done via the AWS CLI.

Option 1: Using the AWS CLI (Update the <values> to reflect the information returned from the deployment.)

aws amplify start-job --app-id <MyAmplifyAppId> --branch-name master --job-type RELEASE

Also available in the stack output as AmplifyDeployCommand

To check on the status, you can view it on the AWS Amplify Console or run:

aws amplify get-job --app-id <MyAmplifyAppId> --branch-name master --job-id <JobId>

Option 2: Amplify Console page

  1. Open the Amplify Console page
  2. On the left side, under All apps, choose Url-Shortner-Client
  3. Click Run build

*Note: this is only required for the first build subsequent client builds will be triggered when updates are committed to your forked repository.

Cleanup

  1. Open the CloudFormation console
  2. Locate a stack named URLShortener
  3. Select the radio option next to it
  4. Select Delete
  5. Select Delete stack to confirm

*Note: If you opted to have access logs (on by default), you may have to delete the S3 bucket manually.

amazon-api-gateway-url-shortener's People

Contributors

alequetzalli avatar amazon-auto avatar csterritt avatar dependabot[bot] avatar jlhood avatar jonca44 avatar julianwood avatar nmoutschen avatar singledigit avatar weisisheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amazon-api-gateway-url-shortener's Issues

AWS Amplify fails at build frontend stage

See dump files below, could you help, please? I've followed the README instructions and everything completed correctly until this last step.

Starting phase: preBuild

Executing command: cd client

2022-04-13T17:25:37.978Z [INFO]: # Executing command: npm ci
2022-04-13T17:25:39.183Z [WARNING]: npm
2022-04-13T17:25:39.184Z [WARNING]: ERR! cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.

2022-04-13T17:25:39.215Z [WARNING]:
2022-04-13T17:25:39.215Z [WARNING]: npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-04-13T17_25_39_184Z-debug.log

2022-04-13T17:25:39.215Z [HELP]: Outputting the npm debug log
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli '/root/.nvm/versions/node/v14.19.0/bin/node',
1 verbose cli '/root/.nvm/versions/node/v14.19.0/bin/npm',
1 verbose cli 'ci'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose npm-session [removed]
5 info prepare initializing installer
6 verbose prepare starting workers
7 verbose prepare installation prefix: /codebuild/output/[removed]/src/amazon-api-gateway-url-shortener/client
8 verbose checkLock verifying package-lock data
9 verbose teardown shutting down workers.
10 info teardown Done in 0s
11 verbose stack Error: cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.
11 verbose stack at Installer.checkLock (/root/.nvm/versions/node/v14.19.0/lib/node_modules/npm/node_modules/libcipm/index.js:165:9)
11 verbose stack at /root/.nvm/versions/node/v14.19.0/lib/node_modules/npm/node_modules/libcipm/index.js:139:16
12 verbose cwd /codebuild/output/[removed]/src/amazon-api-gateway-url-shortener/client
13 verbose Linux 4.14.246-187.474.amzn2.x86_64
14 verbose argv "/root/.nvm/versions/node/v14.19.0/bin/node" "/root/.nvm/versions/node/v14.19.0/bin/npm" "ci"
15 verbose node v14.19.0
16 verbose npm v6.14.16
17 error cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.
18 verbose exit [ 1, true ]

2022-04-13T17:25:39.218Z [ERROR]: !!! Build failed
2022-04-13T17:25:39.218Z [ERROR]: !!! Non-Zero Exit Code detected
2022-04-13T17:25:39.218Z [INFO]: # Starting environment caching...
2022-04-13T17:25:39.219Z [INFO]: # Environment caching completed
Terminating logging...`

Docs Improvement - No screenshot of the UI

Please add a screenshot of the UI. It is not present in the blog series https://aws.amazon.com/it/blogs/compute/building-a-serverless-url-shortener-app-without-lambda-part-1/ nor in this repo.

The only frontend available appears to be here https://github.com/aws-samples/amazon-api-gateway-url-shortener/blob/master/client/src/views/Dashboard.vue but I am not sure people will setup everything if they cannot see the UI before setting up the whole environment. I propose to add at least 1 screenshot of the frontend in the docs.

Can I write the mapping templates (in api.yaml) using block strings?

I'm struggling to figure out the right syntax to use in CloudFormation templates.

Is there a particular reason why the mapping templates were written with double quoted strings, as opposed to block strings?

responseTemplates:
  application/json: "#set($inputRoot = $input.path('$'))[ \
    #foreach($elem in $inputRoot.Items) { \
      \"id\":\"$elem.id.S\", \
      \"url\": \"$elem.url.S\", \
      \"timestamp\": \"$elem.timestamp.S\", \
      \"owner\": \"$elem.owner.S\"} \
    #if($foreach.hasNext),#end \
    #end]"

Using a multiline/block string seems so much more writable and readable:

responseTemplates:
  application/json: |-
    #set($inputRoot = $input.path('$'))[
      #foreach($elem in $inputRoot.Items) {
        "id": "$elem.id.S",
        "url": "$elem.url.S",
        "timestamp": "$elem.timestamp.S",
        "owner": "$elem.owner.S"}
      #if($foreach.hasNext),#end
    #end]

(this example is from api.yaml line 171)

Cognito--access denied on non-local site?

Deploy runs perfectly, and so does Amplify build. Visiting website, both "signup" and "login" give Access denied error. Grubbing around Cognito doesn't surface any settings I can change, registered a user manually but stuck. Welcome any tips, tricks.

Wrong URL generated in output

Hey -- getting a bogus URL generated in the output. In my case, it generated:

VUE_APP_API_ROOT=https://:d2xx9t23blargh.cloudfront.net

Note the colon before the "d2xx9...". Unsurprisingly, this failed when I put it in the .env file for the Vue app, but when I removed the rogue colon and restarted the app, it worked fine.

I don't think I changed anything, other than giving my credentials, etc.

Thanks!

SlipLink cannot be created. Bad format.

Thanks for the great introduction. I just tried it. But I get an error when I want to shorten URLs "SlipLink cannot be created. Bad format.". Any advice for me?

Regards,
Ramón

Perhaps add CLI command to delete the stack?

Thanks for doing this, it's fantastic!

From working on another project, I learned that you can do:

aws cloudformation delete-stack --stack-name yourstacknamehere

This starts a stack deletion, and returns nearly instantly. I don't know how to tell if it's done or not, other than trying to get information about the stack, which will eventually fail. I'll dig around and see if I can find out.

Stack building is failed because of "CloudWatch Logs role ARN must be set in account settings to enable logging"

Dev Environment

SAM CLI, version 1.0.0
aws-cli/2.0.33 Python/3.7.4 Darwin/19.6.0 botocore/2.0.0dev37

When I start the deployment using command sam deploy -g, It is getting failed in between with the following message of failure.

CloudWatch Logs role ARN must be set in account settings to enable logging (Service: AmazonApiGateway; Status Code:400; Error Code: BadRequestException; Request ID: 58541396-e90b-418c-a8c5-8ae495604e75)

I am able to figure out this problem by adding following policy in the template.yml

  CloudWatchRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - apigateway.amazonaws.com
            Action: 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - >-
          arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
  Account:
    Type: 'AWS::ApiGateway::Account'
    Properties:
      CloudWatchRoleArn: !GetAtt 
        - CloudWatchRole
        - Arn

Now it is getting deployed as expected.

After successful deployment I can see the landing Magic home page but when i click on Signup/Login I am getting following error.

<Error> <Code>AccessDenied</Code> <Message>Access Denied</Message> <RequestId>BFD2F9DAC0C76C7E</RequestId> <HostId>u1cmt0WOo96vP9naC7HW5aRd7N7aBzSQse9e0llXByYRQX8B5OIOxtR/JmEjZmw2iQJhvjf33NU=</HostId> </Error>
I am not sure what am I missing here.

Amplify Console: while trigger a build

While building code using Amplify getting following error.

17 error cipm can only install packages with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or later to generate it, then try again.
18 verbose exit [ 1, true ]
2021-05-19T12:16:42.475Z [ERROR]: !!! Build failed
2021-05-19T12:16:42.475Z [ERROR]: !!! Non-Zero Exit Code detected
2021-05-19T12:16:42.475Z [INFO]: # Starting environment caching...
2021-05-19T12:16:42.475Z [INFO]: # Environment caching completed

Thanks!

Docs Missing Instructions for Setting Up Custom Domains?

So on the proper https://slip.link/, the client and "API" are on the same domain and work side by side. I cannot for the life of me figure out how to do this in practice and the docs don't seem to cover it.

If I add both origins to cloudfront and set up the behaviors, I can't get amplify to work because it goes to https://mydoma.in/pkojhuytfyfguhijokpl;njbhgvyt78y92304rr/ where there is a random string after the domain and this breaks cloudfront. No matter what I do with the behaviors, I can't find a way to get it to work.

I also tried to do the static files in S3, but for the life of me I can't get all of the behaviors set up in a way that works with that either.

I'd think that maybe the amplify site would pull the redirects from dynamo, but it doesn't seem to do that. The cloudformation seems to set up the api gateway and only link cloudfront to that api. The API doesn't include amplify though either.

Am I missing something or do the docs just not cover it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.