LoginSignup
1
0

More than 3 years have passed since last update.

概要

最近インフラに興味が湧いてきたので、terrafromを使って遊んでいました。
すると「CDK for Terraform」なるものを発見👀

AWS-CDKを使ってTypeScriptやPythonなどでTerraformを扱えるとのことなので、さっそく試して見ました!

※注意: まだ開発中であり、アルファテスト段階にあるソフトウェアが含まれています。本番環境には使用しないでください🙇‍♂️

hashicorp/terraform-cdk
Write Terraform with Typescript and Python
CDK for Terraform: Enabling Python & TypeScript Support

コマンド

# CLIのインストール
$ npm install -g cdktf-cli

# 作成(プロジェクト配下で)
$ cdktf init --template=typescript

# Terraformのコードを生成
$ cdktf synth

# リソースの生成
$ cdktf deploy

# リソースの削除
$ cdktf destroy

S3を作成してみた

以下のコードで簡単に作成できました。


import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import { AwsProvider, S3Bucket } from './.gen/providers/aws'

class MyStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name);

    new AwsProvider(this, 'aws', {
      region: 'ap-northeast-1'
    });

    // S3 bucket
    new S3Bucket(this, "my_bucket", {
      bucket: 's3-example.com',
      acl: "private"
    })
  }
}

const app = new App();
new MyStack(app, 'aws');
app.synth();
# Terraformのコードを生成
$ cdktf synth

# リソースの生成
$ cdktf deploy

感想

HCL言語だとオプションがいまいちわからなかったんですが、TypeScriptを用いることでd.tsファイルである程度何が必要で何が使えるかなどがわかりやすかったです。

さいごに

続きまして、アルサーガパートナーズ Advent Calendar 2020 16日目の記事は、@miumi さんです :tada:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0