# @serverless/aws-cloudfront

> &nbsp;

Latest version **6.0.0** (published 2019-10-07) · Apache license · 0 weekly downloads

## Install

```sh
npm install @serverless/aws-cloudfront
pnpm add @serverless/aws-cloudfront
yarn add @serverless/aws-cloudfront
bun add @serverless/aws-cloudfront
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2019-10-07 |
| First published | 2019-08-20 |
| Weekly downloads | 0 |
| License | Apache |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 84.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Serverless, Inc. |
| Maintainers | ac360, aj.stuyvenberg, dschep, eahefnawy, pmuens |

## Links

- npm: https://www.npmjs.com/package/@serverless/aws-cloudfront
- npm.io page: https://npm.io/package/@serverless/aws-cloudfront

## Dependencies (3)

- [ramda](https://npm.io/package/ramda.md) ^0.26.1
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.499.0
- [@serverless/core](https://npm.io/package/@serverless/core.md) ^1.0.0

## Recent versions

- 6.0.0 (latest) — 2019-10-07
- 5.0.0 — 2019-09-23
- 3.2.0 — 2019-09-20
- 3.1.0 — 2019-09-05
- 3.0.0 — 2019-09-04
- 2.0.0 — 2019-08-29
- 1.0.5 — 2019-08-21
- 1.0.4 — 2019-08-21
- 1.0.3 — 2019-08-21
- 1.0.2 — 2019-08-20
- 1.0.1 — 2019-08-20
- 1.0.0 — 2019-08-20

## README

# aws-cloudfront

&nbsp;

Deploy an AWS CloudFront distribution for the provided origins using [Serverless Components](https://github.com/serverless/components).

&nbsp;

1. [Install](#1-install)
2. [Create](#2-create)
3. [Configure](#3-configure)
4. [Deploy](#4-deploy)

&nbsp;

### 1. Install

```console
$ npm install -g serverless
```

### 2. Create

```console
$ mkdir cdn
$ cd cdn
```

the directory should look something like this:

```
|- serverless.yml
|- .env      # your AWS api keys

```

```
# .env
AWS_ACCESS_KEY_ID=XXX
AWS_SECRET_ACCESS_KEY=XXX
```

### 3. Configure

```yml
# serverless.yml

distribution:
  component: '@serverless/aws-cloudfront'
  inputs:
    region: us-east-1
    enabled: true # optional
    comment: 'My distribution' # optional
    defaults: # optional
      ttl: 15
      allowedHttpMethods: ['HEAD', 'GET']
      forward: # optional
        # array of header names, 'none' or 'all'
        headers: ['Accept', 'Accept-Language']
        # array of cookie names, 'none' or 'all'
        cookies: ['my-cookie]
        queryString: true
        queryStringCacheKeys: ['queryKey']
      viewerProtocolPolicy: 'https-only' # optional
      smoothStreaming: true # optional
      compress: true # optional
      fieldLevelEncryptionId: '123' # optional
      lambda@edge: # added to cloudfront default cache behavior
        viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version
    origins:
      - https://my-bucket.s3.amazonaws.com
```

#### Custom cache behavior
Custom cache behaviors support the same config parameters as the default cache behavior (see the example above). 
```yml
# serverless.yml

distribution:
  component: '@serverless/aws-cloudfront'
  inputs:
    origins:
      - url: https://my-assets.com
        pathPatterns:
          /static/images: # route any /static/images requests to https://my-assets.com
            ttl: 10
            allowedHttpMethods: ['GET', 'HEAD'] # optional
            forward: # optional
              headers: 'all' 
              cookies: ['auth-token']
              queryString: true
            compress: false # optional
            # ...
```

#### Lambda@Edge

```yml
# serverless.yml

distribution:
  component: '@serverless/aws-cloudfront'
  inputs:
    origins:
      - url: https://sampleorigin.com
        pathPatterns:
          /sample/path:
            ttl: 10
            lambda@edge:
              viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version # lambda ARN including version
```

#### Private S3 Content

To restrict access to content that you serve from S3 you can mark as `private` your S3 origins:

```yml
# serverless.yml

distribution:
  component: '@serverless/aws-cloudfront'
  inputs:
    origins:
      - url: https://my-private-bucket.s3.amazonaws.com
        private: true
```

A bucket policy will be added that grants CloudFront with access to the bucket objects. Note that it doesn't remove any existing permissions on the bucket. If users currently have permission to access the files in your bucket using Amazon S3 URLs you will need to manually remove those.

This is documented in more detail here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html

### 4. Deploy

```console
$ serverless
```

&nbsp;

### New to Components?

Checkout the [Serverless Components](https://github.com/serverless/components) repo for more information.

---
_Source: https://npm.io/package/@serverless/aws-cloudfront · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
