0.0.206 • Published 2 days ago

@mavogel/cdk-hugo-pipeline v0.0.206

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 days ago

cdk-hugo-pipeline

cdk-constructs: experimental npm version

This is an AWS CDK Construct for deploying Hugo Static websites to AWS S3 behind SSL/Cloudfront with cdk-pipelines, having an all-in-one infrastructure-as-code deployment on AWS, meaning

  • self-contained, all resources should be on AWS
  • a blog with hugo and a nice theme (in my opinion)
  • using cdk and cdk-pipelines running
  • a monorepo with all the code components
  • with a development stage on a dev.your-domain.com subdomain

Take a look at the blog post My blog with hugo - all on AWS in which I write about all the details and learnings.

Prerequisites

  1. binaries
brew install node@16 hugo docker
  1. a Route53 Hosted Zone for your-domain.com in the AWS account you deploy into.

If you use hugo modules add them as git submodules in the themes directory, so they can be pulled by the same git command in the codepipeline.

Usage

In this demo case, we will use the blist theme: https://github.com/apvarun/blist-hugo-theme, however you can use any other hugo theme. Note, that you need to adapt the branch of the theme you use.

With a projen template (recommended)

and the blist theme.

mkdir my-blog && cd my-blog

npx projen new \
    --from @mavogel/projen-cdk-hugo-pipeline@~0 \
    --domain your-domain.com \
    --projenrc-ts

npm --prefix blog install
# and start the development server on http://localhost:1313
npm run dev

By hand (more flexible)

cat << EOF > blog/config/production/config.toml

file: blog/config/production/config.toml

baseurl = "https://your-domain.com" publishDir = "public-production" EOF

4. ignore the output folders in the file `blog/.gitignore`
```bash
cat << EOF >> blog/.gitignore
public-*
resources/_gen
node_modules
.DS_Store
.hugo_build.lock
EOF
  1. additionally copy package.jsons. Note: this depends on your theme
cp blog/themes/blist/package.json blog/package.json
cp blog/themes/blist/package-lock.json blog/package-lock.json
  1. Optional: add the script to the .projenrc.ts. Note: the command depends on your theme as well
project.addScripts({
  dev: 'npm --prefix blog run start',
  # below is the general commands
  # dev: 'cd blog && hugo server --watch --buildFuture --cleanDestinationDir --disableFastRender',
});

and update the project via the following command

npm run projen

Use Typescript and deploy to your AWS account

Add this to the the main.ts file

import { App, Stack, StackProps } from 'aws-cdk-lib';
import { HugoPipeline } from '@mavogel/cdk-hugo-pipeline';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // we only need 1 stack as it creates dev and prod stage in the pipeline
    new HugoPipeline(this, 'my-blog', {
      domainName: 'your-domain.com', // <- adapt here
    });
}

and adapt the main.test.ts (yes, known issue. See #40)

test('Snapshot', () => {
  expect(true).toBe(true);
});

which has a Route53 Hosted Zone for your-domain.com:

Deploy it

# build it locally via
npm run build
# deploy the repository and the pipeline once via
npm run deploy
  1. This will create the codecommit repository and the codepipeline. The pipeline will fail first, so now commit the code.
# add the remote, e.g. via GRPC http
git remote add origin codecommit::<aws-region>://your-blog
# rename the branch to master (wlll fix this)
git branch -m master main
# push the code
git push origin master
  1. ... wait until the pipeline has deployed to the dev stage, go to your url dev.your-comain.com, enter the basic auth credentials (default: john:doe) and look at you beautiful blog :tada:

Customizations

Redirects

You can add customizations such as HTTP 301 redirects , for example 1. from /talks/ to /works/: 1. from https://your-domain.com/talks/2024-01-24-my-talk 2. to https://your-domain.com/works/2024-01-24-my-talk 2. or more complex ones /post/2024-01-25-my-blog/gallery/my-image.webp to /images/2024-01-25-my-blog/my-image.webp, which is represented by the regexp '/(\.\*)(\\\/post\\\/)(\.\*)(\\\/gallery\\\/)(\.\*)/' and capture group '$1/images/$3/$5'. Here as full example: 1. from https://your-domain.com/post/2024-01-25-my-blog/gallery/my-image.webp 2. to https://your-domain.com/images/2024-01-25-my-blog/my-image.webp

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // Note: test you regex upfront 
    // here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
    // an escape them.
    
    new HugoPipeline(this, 'my-blog', {
      domainName: 'your-domain.com', // <- adapt here
      cloudfrontRedirectReplacements: { // <- all regexp need to be escaped!
        '/\\\/talks\\\//': '/works/',  // /talks/ -> /\\\/talks\\\//
        // /(.*)(\/post\/)(.*)(\/gallery\/)(.*)/
        '/(\.\*)(\\\/post\\\/)(\.\*)(\\\/gallery\\\/)(\.\*)/': '$1/images/$3/$5',
      },
    });
}

However, you can also pass in a whole custom functions as the next section shows.

Custom Cloudfront function

For the VIEWER_REQUEST, where you can also achieve Basic Auth or redirects the way you want

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const customCfFunctionCodeDevelopment = `
function handler(event) {
    var request = event.request;
    var uri = request.uri;
    var authHeaders = request.headers.authorization;

    var regexes = [/\/talks\//, /\/post\//];

    if (regexes.some(regex => regex.test(request.uri))) {
        request.uri = request.uri.replace(/\/talks\//, '/works/');
        request.uri = request.uri.replace(/\/post\//, '/posts/');

        var response = {
            statusCode: 301,
            statusDescription: "Moved Permanently",
            headers:
                { "location": { "value": request.uri } }
        }
        return response;
    }

    var expected = "Basic am9objpkb2U=";

    if (authHeaders && authHeaders.value === expected) {
        if (uri.endsWith('/')) {
            request.uri += 'index.html';
        }
        else if (!uri.includes('.')) {
            request.uri += '/index.html';
        }
        return request;
    }

    var response = {
        statusCode: 401,
        statusDescription: "Unauthorized",
        headers: {
            "www-authenticate": {
                value: 'Basic realm="Enter credentials for this super secure site"',
            },
        },
    };

    return response;
} 
`

    const customCfFunctionCodeProduction = `
function handler(event) {
  var request = event.request;
  var uri = request.uri;

  if (uri.endsWith('/')) {
    request.uri += 'index.html';
  }
  else if (!uri.includes('.')) {
    request.uri += '/index.html';
  }

  return request;
}
`
    // we do the escapes here so it passed in correctly
    const escapedtestCfFunctionCodeDevelopment = customCfFunctionCodeDevelopment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
    const escapedtestCfFunctionCodeProduction = customCfFunctionCodeProduction.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
    
    new HugoPipeline(this, 'my-blog', {
      domainName: 'your-domain.com', // <- adapt here
      // Note: keep in sync with the basic auth defined in the function
      // echo -n "john:doe"|base64 -> 'am9objpkb2U='
      basicAuthUsername: 'john',
      basicAuthPassword: 'doe',
      cloudfrontCustomFunctionCodeDevelopment: cloudfront.FunctionCode.fromInline(escapedtestCfFunctionCodeDevelopment),
      cloudfrontCustomFunctionCodeProduction: cloudfront.FunctionCode.fromInline(escapedtestCfFunctionCodeProduction),
    });
}

Known issues

  • If with npm test you get the error docker exited with status 1,
    • then clean the docker layers and re-run the tests via docker system prune -f
    • and if it happens in codebuild, re-run the build

Open todos

  • a local development possibility in docker

Resources / Inspiration

0.0.205

3 days ago

0.0.204

4 days ago

0.0.203

5 days ago

0.0.206

2 days ago

0.0.202

6 days ago

0.0.201

8 days ago

0.0.200

9 days ago

0.0.199

11 days ago

0.0.198

12 days ago

0.0.197

13 days ago

0.0.196

14 days ago

0.0.195

16 days ago

0.0.194

17 days ago

0.0.193

18 days ago

0.0.192

19 days ago

0.0.191

20 days ago

0.0.190

25 days ago

0.0.189

26 days ago

0.0.188

29 days ago

0.0.187

30 days ago

0.0.186

1 month ago

0.0.185

1 month ago

0.0.184

1 month ago

0.0.183

1 month ago

0.0.182

1 month ago

0.0.181

1 month ago

0.0.179

1 month ago

0.0.180

1 month ago

0.0.178

1 month ago

0.0.177

1 month ago

0.0.176

1 month ago

0.0.175

1 month ago

0.0.174

2 months ago

0.0.173

2 months ago

0.0.172

2 months ago

0.0.171

2 months ago

0.0.170

2 months ago

0.0.169

2 months ago

0.0.168

2 months ago

0.0.167

2 months ago

0.0.166

2 months ago

0.0.165

2 months ago

0.0.164

2 months ago

0.0.163

2 months ago

0.0.162

2 months ago

0.0.161

2 months ago

0.0.160

2 months ago

0.0.159

2 months ago

0.0.158

2 months ago

0.0.157

2 months ago

0.0.156

2 months ago

0.0.155

2 months ago

0.0.154

2 months ago

0.0.153

2 months ago

0.0.152

2 months ago

0.0.151

2 months ago

0.0.150

2 months ago

0.0.149

2 months ago

0.0.148

2 months ago

0.0.147

2 months ago

0.0.146

3 months ago

0.0.145

3 months ago

0.0.144

3 months ago

0.0.143

3 months ago

0.0.142

3 months ago

0.0.141

3 months ago

0.0.140

3 months ago

0.0.139

3 months ago

0.0.138

3 months ago

0.0.137

3 months ago

0.0.136

3 months ago

0.0.135

3 months ago

0.0.134

3 months ago

0.0.133

3 months ago

0.0.132

3 months ago

0.0.131

3 months ago

0.0.126

3 months ago

0.0.125

3 months ago

0.0.124

3 months ago

0.0.123

3 months ago

0.0.122

3 months ago

0.0.121

3 months ago

0.0.120

3 months ago

0.0.119

3 months ago

0.0.117

3 months ago

0.0.118

3 months ago

0.0.116

3 months ago

0.0.115

3 months ago

0.0.114

4 months ago

0.0.113

4 months ago

0.0.112

4 months ago

0.0.111

4 months ago

0.0.110

4 months ago

0.0.109

4 months ago

0.0.108

4 months ago

0.0.106

4 months ago

0.0.105

4 months ago

0.0.104

4 months ago

0.0.103

4 months ago

0.0.107

4 months ago

0.0.102

4 months ago

0.0.99

4 months ago

0.0.101

4 months ago

0.0.100

4 months ago

0.0.95

4 months ago

0.0.96

4 months ago

0.0.97

4 months ago

0.0.98

4 months ago

0.0.93

4 months ago

0.0.94

4 months ago

0.0.92

4 months ago

0.0.91

4 months ago

0.0.90

4 months ago

0.0.88

4 months ago

0.0.89

4 months ago

0.0.86

5 months ago

0.0.87

5 months ago

0.0.85

5 months ago

0.0.84

5 months ago

0.0.83

5 months ago

0.0.82

5 months ago

0.0.81

5 months ago

0.0.80

5 months ago

0.0.79

5 months ago

0.0.77

5 months ago

0.0.78

5 months ago

0.0.75

6 months ago

0.0.76

5 months ago

0.0.74

6 months ago

0.0.73

6 months ago

0.0.72

6 months ago

0.0.71

6 months ago

0.0.70

6 months ago

0.0.69

6 months ago

0.0.68

6 months ago

0.0.67

6 months ago

0.0.66

6 months ago

0.0.65

6 months ago

0.0.64

6 months ago

0.0.63

6 months ago

0.0.62

6 months ago

0.0.61

6 months ago

0.0.60

6 months ago

0.0.59

6 months ago

0.0.58

6 months ago

0.0.57

6 months ago

0.0.56

6 months ago

0.0.55

6 months ago

0.0.54

7 months ago

0.0.53

7 months ago

0.0.52

7 months ago

0.0.51

7 months ago

0.0.50

7 months ago

0.0.49

7 months ago

0.0.48

7 months ago

0.0.47

7 months ago

0.0.46

7 months ago

0.0.45

7 months ago

0.0.44

7 months ago

0.0.43

7 months ago

0.0.42

7 months ago

0.0.41

7 months ago

0.0.40

7 months ago

0.0.39

7 months ago

0.0.38

7 months ago

0.0.37

7 months ago

0.0.36

8 months ago

0.0.35

8 months ago

0.0.34

8 months ago

0.0.33

8 months ago

0.0.32

8 months ago

0.0.31

8 months ago

0.0.30

8 months ago

0.0.29

8 months ago

0.0.28

8 months ago

0.0.27

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

10 months ago

0.0.23

10 months ago

0.0.22

10 months ago

0.0.21

10 months ago

0.0.20

10 months ago

0.0.19

10 months ago

0.0.18

10 months ago

0.0.17

10 months ago

0.0.16

10 months ago

0.0.15

11 months ago

0.0.14

11 months ago