0.0.412 • Published 5 months ago

@mavogel/cdk-hugo-pipeline v0.0.412

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

cdk-hugo-pipeline

Source Build Status ESLint Code Formatting Latest release GitHub npm typescript cdk-constructs: experimental

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

🚀 Unlock the Full Potential of Your AWS Cloud Infrastructure

Hi, I’m Manuel, an AWS expert passionate about empowering businesses with scalable, resilient, and cost-optimized cloud solutions. With MV Consulting, I specialize in crafting tailored AWS architectures and DevOps-driven workflows that not only meet your current needs but grow with you.


🌟 Why Work With Me?

✔️ Tailored AWS Solutions: Every business is unique, so I design custom solutions that fit your goals and challenges.
✔️ Well-Architected Designs: From scalability to security, my solutions align with AWS Well-Architected Framework.
✔️ Cloud-Native Focus: I specialize in modern, cloud-native systems that embrace the full potential of AWS.
✔️ Business-Driven Tech: Technology should serve your business, not the other way around.


🛠 What I Bring to the Table

🔑 12x AWS Certifications
I’m AWS Certified Solutions Architect and DevOps – Professional and hold numerous additional certifications, so you can trust I’ll bring industry best practices to your projects. Feel free to explose by badges

⚙️ Infrastructure as Code (IaC)
With deep expertise in AWS CDK and Terraform, I ensure your infrastructure is automated, maintainable, and scalable.

📦 DevOps Expertise
From CI/CD pipelines with GitHub Actions and GitLab CI to container orchestration Kubernetes and others, I deliver workflows that are smooth and efficient.

🌐 Hands-On Experience
With over 7 years of AWS experience and a decade in the tech world, I’ve delivered solutions for companies large and small. My open-source contributions showcase my commitment to transparency and innovation. Feel free to explore my GitHub profile


💼 Let’s Build Something Great Together

I know that choosing the right partner is critical to your success. When you work with me, you’re not just contracting an engineer – you’re gaining a trusted advisor and hands-on expert who cares about your business as much as you do.

✔️ Direct Collaboration: No middlemen or red tape – you work with me directly.
✔️ Transparent Process: Expect open communication, clear timelines, and visible results.
✔️ Real Value: My solutions focus on delivering measurable impact for your business.


🙌 Acknowledgements

Big shoutout to the amazing team behind Projen!
Their groundbreaking work simplifies cloud infrastructure projects and inspires us every day. 💡

Author

Manuel Vogel

npm.io npm.io

0.0.339

1 year ago

0.0.348

12 months ago

0.0.347

12 months ago

0.0.346

12 months ago

0.0.345

12 months ago

0.0.349

12 months ago

0.0.340

1 year ago

0.0.344

12 months ago

0.0.343

12 months ago

0.0.342

12 months ago

0.0.341

12 months ago

0.0.403

9 months ago

0.0.402

9 months ago

0.0.401

9 months ago

0.0.400

9 months ago

0.0.407

7 months ago

0.0.406

8 months ago

0.0.405

8 months ago

0.0.404

8 months ago

0.0.412

5 months ago

0.0.411

5 months ago

0.0.410

5 months ago

0.0.409

6 months ago

0.0.408

6 months ago

0.0.395

9 months ago

0.0.394

9 months ago

0.0.393

10 months ago

0.0.392

10 months ago

0.0.399

9 months ago

0.0.398

9 months ago

0.0.397

9 months ago

0.0.396

9 months ago

0.0.391

10 months ago

0.0.390

10 months ago

0.0.359

11 months ago

0.0.358

11 months ago

0.0.357

12 months ago

0.0.356

12 months ago

0.0.351

12 months ago

0.0.350

12 months ago

0.0.355

12 months ago

0.0.354

12 months ago

0.0.353

12 months ago

0.0.352

12 months ago

0.0.369

11 months ago

0.0.368

11 months ago

0.0.367

11 months ago

0.0.362

11 months ago

0.0.361

11 months ago

0.0.360

11 months ago

0.0.366

11 months ago

0.0.365

11 months ago

0.0.364

11 months ago

0.0.363

11 months ago

0.0.379

10 months ago

0.0.378

10 months ago

0.0.373

11 months ago

0.0.372

11 months ago

0.0.371

11 months ago

0.0.370

11 months ago

0.0.377

10 months ago

0.0.376

11 months ago

0.0.375

11 months ago

0.0.374

11 months ago

0.0.389

10 months ago

0.0.384

10 months ago

0.0.383

10 months ago

0.0.382

10 months ago

0.0.381

10 months ago

0.0.388

10 months ago

0.0.387

10 months ago

0.0.386

10 months ago

0.0.385

10 months ago

0.0.380

10 months ago

0.0.338

1 year ago

0.0.337

1 year ago

0.0.336

1 year ago

0.0.335

1 year ago

0.0.334

1 year ago

0.0.333

1 year ago

0.0.332

1 year ago

0.0.331

1 year ago

0.0.330

1 year ago

0.0.329

1 year ago

0.0.328

1 year ago

0.0.326

1 year ago

0.0.325

1 year ago

0.0.324

1 year ago

0.0.323

1 year ago

0.0.327

1 year ago

0.0.322

1 year ago

0.0.315

1 year ago

0.0.314

1 year ago

0.0.313

1 year ago

0.0.312

1 year ago

0.0.319

1 year ago

0.0.318

1 year ago

0.0.317

1 year ago

0.0.316

1 year ago

0.0.311

1 year ago

0.0.310

1 year ago

0.0.309

1 year ago

0.0.321

1 year ago

0.0.320

1 year ago

0.0.308

1 year ago

0.0.307

1 year ago

0.0.306

1 year ago

0.0.305

1 year ago

0.0.304

1 year ago

0.0.303

1 year ago

0.0.302

1 year ago

0.0.301

1 year ago

0.0.300

1 year ago

0.0.299

1 year ago

0.0.298

1 year ago

0.0.296

1 year ago

0.0.295

1 year ago

0.0.294

1 year ago

0.0.293

1 year ago

0.0.297

1 year ago

0.0.292

1 year ago

0.0.291

1 year ago

0.0.209

1 year ago

0.0.208

1 year ago

0.0.216

1 year ago

0.0.215

1 year ago

0.0.214

1 year ago

0.0.213

1 year ago

0.0.219

1 year ago

0.0.218

1 year ago

0.0.217

1 year ago

0.0.212

1 year ago

0.0.211

1 year ago

0.0.210

1 year ago

0.0.227

1 year ago

0.0.226

1 year ago

0.0.225

1 year ago

0.0.224

1 year ago

0.0.229

1 year ago

0.0.228

1 year ago

0.0.223

1 year ago

0.0.222

1 year ago

0.0.221

1 year ago

0.0.220

1 year ago

0.0.279

1 year ago

0.0.274

1 year ago

0.0.273

1 year ago

0.0.272

1 year ago

0.0.271

1 year ago

0.0.278

1 year ago

0.0.277

1 year ago

0.0.276

1 year ago

0.0.275

1 year ago

0.0.270

1 year ago

0.0.285

1 year ago

0.0.284

1 year ago

0.0.283

1 year ago

0.0.282

1 year ago

0.0.289

1 year ago

0.0.288

1 year ago

0.0.287

1 year ago

0.0.286

1 year ago

0.0.281

1 year ago

0.0.280

1 year ago

0.0.290

1 year ago

0.0.238

1 year ago

0.0.237

1 year ago

0.0.236

1 year ago

0.0.235

1 year ago

0.0.239

1 year ago

0.0.230

1 year ago

0.0.234

1 year ago

0.0.233

1 year ago

0.0.232

1 year ago

0.0.231

1 year ago

0.0.249

1 year ago

0.0.248

1 year ago

0.0.247

1 year ago

0.0.246

1 year ago

0.0.241

1 year ago

0.0.240

1 year ago

0.0.245

1 year ago

0.0.244

1 year ago

0.0.243

1 year ago

0.0.242

1 year ago

0.0.259

1 year ago

0.0.258

1 year ago

0.0.257

1 year ago

0.0.252

1 year ago

0.0.251

1 year ago

0.0.250

1 year ago

0.0.256

1 year ago

0.0.255

1 year ago

0.0.254

1 year ago

0.0.253

1 year ago

0.0.269

1 year ago

0.0.268

1 year ago

0.0.263

1 year ago

0.0.262

1 year ago

0.0.261

1 year ago

0.0.260

1 year ago

0.0.267

1 year ago

0.0.266

1 year ago

0.0.265

1 year ago

0.0.264

1 year ago

0.0.207

1 year ago

0.0.205

1 year ago

0.0.204

1 year ago

0.0.203

1 year ago

0.0.206

1 year ago

0.0.202

1 year ago

0.0.201

1 year ago

0.0.200

1 year ago

0.0.199

1 year ago

0.0.198

1 year ago

0.0.197

1 year ago

0.0.196

1 year ago

0.0.195

2 years ago

0.0.194

2 years ago

0.0.193

2 years ago

0.0.192

2 years ago

0.0.191

2 years ago

0.0.190

2 years ago

0.0.189

2 years ago

0.0.188

2 years ago

0.0.187

2 years ago

0.0.186

2 years ago

0.0.185

2 years ago

0.0.184

2 years ago

0.0.183

2 years ago

0.0.182

2 years ago

0.0.181

2 years ago

0.0.179

2 years ago

0.0.180

2 years ago

0.0.178

2 years ago

0.0.177

2 years ago

0.0.176

2 years ago

0.0.175

2 years ago

0.0.174

2 years ago

0.0.173

2 years ago

0.0.172

2 years ago

0.0.171

2 years ago

0.0.170

2 years ago

0.0.169

2 years ago

0.0.168

2 years ago

0.0.167

2 years ago

0.0.166

2 years ago

0.0.165

2 years ago

0.0.164

2 years ago

0.0.163

2 years ago

0.0.162

2 years ago

0.0.161

2 years ago

0.0.160

2 years ago

0.0.159

2 years ago

0.0.158

2 years ago

0.0.157

2 years ago

0.0.156

2 years ago

0.0.155

2 years ago

0.0.154

2 years ago

0.0.153

2 years ago

0.0.152

2 years ago

0.0.151

2 years ago

0.0.150

2 years ago

0.0.149

2 years ago

0.0.148

2 years ago

0.0.147

2 years ago

0.0.146

2 years ago

0.0.145

2 years ago

0.0.144

2 years ago

0.0.143

2 years ago

0.0.142

2 years ago

0.0.141

2 years ago

0.0.140

2 years ago

0.0.139

2 years ago

0.0.138

2 years ago

0.0.137

2 years ago

0.0.136

2 years ago

0.0.135

2 years ago

0.0.134

2 years ago

0.0.133

2 years ago

0.0.132

2 years ago

0.0.131

2 years ago

0.0.126

2 years ago

0.0.125

2 years ago

0.0.124

2 years ago

0.0.123

2 years ago

0.0.122

2 years ago

0.0.121

2 years ago

0.0.120

2 years ago

0.0.119

2 years ago

0.0.117

2 years ago

0.0.118

2 years ago

0.0.116

2 years ago

0.0.115

2 years ago

0.0.114

2 years ago

0.0.113

2 years ago

0.0.112

2 years ago

0.0.111

2 years ago

0.0.110

2 years ago

0.0.109

2 years ago

0.0.108

2 years ago

0.0.106

2 years ago

0.0.105

2 years ago

0.0.104

2 years ago

0.0.103

2 years ago

0.0.107

2 years ago

0.0.102

2 years ago

0.0.99

2 years ago

0.0.101

2 years ago

0.0.100

2 years ago

0.0.95

2 years ago

0.0.96

2 years ago

0.0.97

2 years ago

0.0.98

2 years ago

0.0.93

2 years ago

0.0.94

2 years ago

0.0.92

2 years ago

0.0.91

2 years ago

0.0.90

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.86

2 years ago

0.0.87

2 years ago

0.0.85

2 years ago

0.0.84

2 years ago

0.0.83

2 years ago

0.0.82

2 years ago

0.0.81

2 years ago

0.0.80

2 years ago

0.0.79

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.74

2 years ago

0.0.73

2 years ago

0.0.72

2 years ago

0.0.71

2 years ago

0.0.70

2 years ago

0.0.69

2 years ago

0.0.68

2 years ago

0.0.67

2 years ago

0.0.66

2 years ago

0.0.65

2 years ago

0.0.64

2 years ago

0.0.63

2 years ago

0.0.62

2 years ago

0.0.61

2 years ago

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.53

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.48

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago