Communitygithub.com

hashicorp/provider-resources

Plugin Framework를 사용하여 Terraform Provider 리소스 및 데이터 소스를 구현합니다. Provider 리소스의 CRUD 작업, 스키마 설계, 상태 관리 및 승인 테스트 개발 시 사용합니다.

provider-resources란 무엇인가요?

이 스킬은 HashiCorp Terraform Provider 개발 시 Plugin Framework를 활용하여 리소스와 데이터 소스를 구현하는 방법을 안내합니다. Go 언어 기반의 Terraform Provider에서 CRUD(Create, Read, Update, Delete) 작업, 스키마 설계, 상태 관리(state management), 그리고 승인 테스트(acceptance testing)를 단계별로 다룹니다. 개발자는 이 스킬을 통해 Provider의 핵심 기능을 정의하고, Terraform 사용자가 인프라를 선언적으로 관리할 수 있도록 지원합니다. Terraform Plugin Framework를 사용하므로, 기존 SDKv2와의 호환성 및 마이그레이션 고려 사항도 포함됩니다. 주요 사용 사례로는 사용자 정의 클라우드 리소스, API 연동, 또는 기존 인프라를 Terraform으로 관리할 필요가 있을 때 유용합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/hashicorp/agent-skills/tree/main/skills/provider-resources

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

provider-resources은(는) 무엇을 하나요?

Plugin Framework를 사용하여 Terraform Provider 리소스 및 데이터 소스를 구현합니다. Provider 리소스의 CRUD 작업, 스키마 설계, 상태 관리 및 승인 테스트 개발 시 사용합니다.

개요

이 문서는 Terraform Provider를 Plugin Framework를 기반으로 개발할 때 필요한 리소스와 데이터 소스 구현 방법을 설명합니다. CRUD 작업, 스키마 설계, 상태 동기화, 그리고 승인 테스트 작성에 대한 가이드를 제공합니다.

주요 개념

  • 리소스(Resource): Terraform이 관리하는 인프라 객체를 나타냅니다. Create, Read, Update, Delete 작업을 정의합니다.
  • 데이터 소스(Data Source): 읽기 전용 작업으로, 외부 시스템의 데이터를 조회하여 Terraform 구성에서 사용할 수 있게 합니다.
  • 스키마(Schema): 리소스나 데이터 소스의 속성(attribute)을 정의합니다. 타입, 필수 여부, 유효성 검증 등을 지정합니다.
  • 상태 관리(State Management): Terraform 상태 파일에 리소스의 현재 상태를 저장하고 동기화합니다.
  • 승인 테스트(Acceptance Testing): 실제 Provider와 통신하여 리소스 생성, 수정, 삭제 등이 정상 동작하는지 검증합니다.

구현 단계

1. 리소스 구현

package resources

import (
    "context"
    "github.com/hashicorp/terraform-plugin-framework/resource"
    "github.com/hashicorp/terraform-plugin-framework/resource/schema"
)

var _ resource.Resource = &ExampleResource{}

type ExampleResource struct{}

func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
    resp.TypeName = req.ProviderTypeName + "_example"
}

func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            "name": schema.StringAttribute{
                Required: true,
            },
            "description": schema.StringAttribute{
                Optional: true,
            },
        },
    }
}

func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
    // CRUD 구현
}

func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
    // 상태 읽기
}

func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
    // 업데이트
}

func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
    // 삭제
}

2. 데이터 소스 구현

package datasources

import (
    "context"
    "github.com/hashicorp/terraform-plugin-framework/datasource"
    "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

var _ datasource.DataSource = &ExampleDataSource{}

type ExampleDataSource struct{}

func (d *ExampleDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
    resp.TypeName = req.ProviderTypeName + "_example"
}

func (d *ExampleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            "id": schema.StringAttribute{
                Required: true,
            },
            "name": schema.StringAttribute{
                Computed: true,
            },
        },
    }
}

func (d *ExampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
    // 데이터 조회
}

승인 테스트 작성

package provider

import (
    "testing"
    "github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccExampleResource(t *testing.T) {
    resource.Test(t, resource.TestCase{
        ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
        Steps: []resource.TestStep{
            {
                Config: testAccExampleResourceConfig(),
                Check: resource.ComposeAggregateTestCheckFunc(
                    resource.TestCheckResourceAttrSet("example_example.test", "name"),
                ),
            },
        },
    })
}

참고 자료

Individual skills in this repo

This repo contains 15 individual skills — each has its own dedicated page.

hashicorp/aws-ami-builder

Build Amazon Machine Images (AMIs) with Packer using the amazon-ebs builder. Use when creating custom AMIs for EC2 instances.

hashicorp/azure-image-builder

Build Azure managed images and Azure Compute Gallery images with Packer. Use when creating custom images for Azure VMs.

hashicorp/azure-verified-modules

Azure Verified Modules (AVM) requirements and best practices for developing certified Azure Terraform modules. Use when creating or reviewing Azure modules that need AVM certification.

hashicorp/new-terraform-provider

Use this when scaffolding a new Terraform provider.

hashicorp/provider-actions

Implement Terraform Provider actions using the Plugin Framework. Use when developing imperative operations that execute at lifecycle events (before/after create, update, destroy).

hashicorp/provider-docs

Create, update, and review Terraform provider documentation for Terraform Registry using HashiCorp-recommended patterns, tfplugindocs templates, and schema descriptions. Use when adding or changing provider configuration, resources, data sources, ephemeral resources, list resources, functions, or guides; when validating generated docs; and when troubleshooting missing or incorrect Registry documentation.

hashicorp/provider-test-patterns

Terraform provider acceptance test patterns using terraform-plugin-testing with the Plugin Framework. Covers test structure, TestCase/TestStep fields, ConfigStateChecks with custom statecheck.StateCheck implementations, plan checks, CompareValue for cross-step assertions, config helpers, import testing with ImportStateKind, sweepers, and scenario patterns (basic, update, disappears, validation, regression), and ephemeral resource testing with the echoprovider package. Use when writing, reviewing, or debugging provider acceptance tests, including questions about statecheck, plancheck, TestCheckFunc, CheckDestroy, ExpectError, import state verification, ephemeral resources, or how to structure test files.

hashicorp/push-to-registry

Push Packer build metadata to HCP Packer registry for tracking and managing image lifecycle. Use when integrating Packer builds with HCP Packer for version control and governance.

hashicorp/refactor-module

Transform monolithic Terraform configurations into reusable, maintainable modules following HashiCorp's module design principles and community best practices.

hashicorp/run-acceptance-tests

Guide for running acceptance tests for a Terraform provider. Use this when asked to run an acceptance test or to run a test with the prefix `TestAcc`.

hashicorp/terraform-search-import

Discover existing cloud resources using Terraform Search queries and bulk import them into Terraform management. Use when bringing unmanaged infrastructure under Terraform control, auditing cloud resources, or migrating to IaC.

hashicorp/terraform-stacks

Comprehensive guide for working with HashiCorp Terraform Stacks. Use when creating, modifying, or validating Terraform Stack configurations (.tfcomponent.hcl, .tfdeploy.hcl files), working with stack components and deployments from local modules, public registry, or private registry sources, managing multi-region or multi-environment infrastructure, or troubleshooting Terraform Stacks syntax and structure.

hashicorp/terraform-style-guide

Generate Terraform HCL code following HashiCorp's official style conventions and best practices. Use when writing, reviewing, or generating Terraform configurations.

hashicorp/terraform-test

Comprehensive guide for writing and running Terraform tests. Use when creating test files (.tftest.hcl), writing test scenarios with run blocks, validating infrastructure behavior with assertions, mocking providers and data sources, testing module outputs and resource configurations, or troubleshooting Terraform test syntax and execution.

hashicorp/windows-builder

Build Windows images with Packer using WinRM communicator and PowerShell provisioners. Use when creating Windows AMIs, Azure images, or VMware templates.

관련 스킬