

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Memulai dengan AWS DevOps Agen menggunakan AWS CloudFormation
<a name="getting-started-with-aws-devops-agent-getting-started-with-aws-devops-agent-using-aws-cloudformation"></a>

## Ikhtisar
<a name="overview"></a>

Panduan ini menunjukkan cara menggunakan AWS CloudFormation templat untuk membuat dan menyebarkan sumber daya AWS DevOps Agen. Template mengotomatiskan pembuatan ruang agen, peran AWS Identity and Access Management (IAM), aplikasi operator, dan asosiasi AWS akun sebagai infrastruktur sebagai kode.

 CloudFormation Pendekatan ini mengotomatiskan langkah-langkah manual yang dijelaskan dalam panduan [orientasi CLI](https://docs.aws.amazon.com/devopsagent/latest/userguide/getting-started-with-aws-devops-agent-cli-onboarding-guide.html) dengan mendefinisikan semua sumber daya yang diperlukan dalam templat YAMAL deklaratif.

AWS DevOps Agen tersedia di 6 AWS Wilayah berikut: AS Timur (Virginia N.), AS Barat (Oregon), Asia Pasifik (Sydney), Asia Pasifik (Tokyo), Eropa (Frankfurt), dan Eropa (Irlandia). Untuk informasi selengkapnya tentang Wilayah yang didukung, lihat[Wilayah yang Didukung](about-aws-devops-agent-supported-regions.md).

## Prasyarat
<a name="prerequisites"></a>

Sebelum Anda mulai, pastikan Anda memiliki yang berikut:
+ AWS Command Line Interface (AWS CLI) diinstal dan dikonfigurasi dengan kredensi yang sesuai
+ Izin untuk membuat peran dan tumpukan IAM CloudFormation 
+ Satu AWS akun untuk akun pemantauan (primer)
+ (Opsional) AWS Akun kedua jika Anda ingin mengatur pemantauan lintas akun

## Apa yang dicakup oleh panduan ini
<a name="what-this-guide-covers"></a>

Panduan ini dibagi menjadi dua bagian:
+ **Bagian 1** — Menyebarkan ruang agen dengan aplikasi operator dan AWS asosiasi di akun pemantauan Anda. Setelah Anda menyelesaikan bagian ini, agen dapat memantau masalah di akun itu.
+ **Bagian 2 (Opsional)** - Menyebarkan peran IAM lintas akun ke akun sekunder dan tambahkan asosiasi sumber. AWS Konfigurasi ini memungkinkan ruang agen untuk memantau sumber daya di seluruh akun.

## Bagian 1: Menyebarkan ruang agen
<a name="part-1-deploy-the-agent-space"></a>

Di bagian ini, Anda membuat CloudFormation templat yang menyediakan ruang agen, peran IAM, aplikasi operator, dan AWS asosiasi di akun pemantauan Anda.

### Langkah 1: Buat CloudFormation template
<a name="step-1-create-the-cloudformation-template"></a>

Simpan template berikut sebagai`devops-agent-stack.yaml`:

```
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS DevOps Agent - Agent Space with IAM roles, operator app, and AWS association

Parameters:
  AgentSpaceName:
    Type: String
    Default: MyCloudFormationAgentSpace
    Description: Name for the agent space
  AgentSpaceDescription:
    Type: String
    Default: Agent space deployed with CloudFormation
    Description: Description for the agent space

Resources:
  # IAM role assumed by the DevOps Agent service to monitor the account
  DevOpsAgentSpaceRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: DevOpsAgentRole-AgentSpace
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: aidevops.amazonaws.com
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                aws:SourceAccount: !Ref AWS::AccountId
              ArnLike:
                aws:SourceArn: !Sub arn:aws:aidevops:${AWS::Region}:${AWS::AccountId}:agentspace/*
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AIDevOpsAgentAccessPolicy
      Policies:
        - PolicyName: AllowCreateServiceLinkedRoles
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: AllowCreateServiceLinkedRoles
                Effect: Allow
                Action:
                  - iam:CreateServiceLinkedRole
                Resource:
                  - !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/resource-explorer-2.amazonaws.com/AWSServiceRoleForResourceExplorer

  # IAM role for the operator app interface
  DevOpsOperatorRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: DevOpsAgentRole-WebappAdmin
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: aidevops.amazonaws.com
            Action:
              - sts:AssumeRole
              - sts:TagSession
            Condition:
              StringEquals:
                aws:SourceAccount: !Ref AWS::AccountId
              ArnLike:
                aws:SourceArn: !Sub arn:aws:aidevops:${AWS::Region}:${AWS::AccountId}:agentspace/*
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AIDevOpsOperatorAppAccessPolicy

  # The agent space resource
  AgentSpace:
    Type: AWS::DevOpsAgent::AgentSpace
    DependsOn:
      - DevOpsAgentSpaceRole
      - DevOpsOperatorRole
    Properties:
      Name: !Ref AgentSpaceName
      Description: !Ref AgentSpaceDescription
      OperatorApp:
        Iam:
          OperatorAppRoleArn: !GetAtt DevOpsOperatorRole.Arn

  # Association linking the monitoring account to the agent space
  MonitorAssociation:
    Type: AWS::DevOpsAgent::Association
    Properties:
      AgentSpaceId: !GetAtt AgentSpace.AgentSpaceId
      ServiceId: aws
      Configuration:
        Aws:
          AssumableRoleArn: !GetAtt DevOpsAgentSpaceRole.Arn
          AccountId: !Ref AWS::AccountId
          AccountType: monitor

Outputs:
  AgentSpaceId:
    Description: The agent space ID
    Value: !GetAtt AgentSpace.AgentSpaceId
  AgentSpaceArn:
    Description: The agent space ARN
    Value: !GetAtt AgentSpace.Arn
  AgentSpaceRoleArn:
    Description: The agent space IAM role ARN
    Value: !GetAtt DevOpsAgentSpaceRole.Arn
  OperatorRoleArn:
    Description: The operator app IAM role ARN
    Value: !GetAtt DevOpsOperatorRole.Arn
```

### Langkah 2: Menyebarkan tumpukan
<a name="step-2-deploy-the-stack"></a>

Jalankan perintah berikut untuk menyebarkan tumpukan. Ganti `<REGION>` dengan [Wilayah yang Didukung](about-aws-devops-agent-supported-regions.md) (misalnya,`us-east-1`).

```
aws cloudformation deploy \
  --template-file devops-agent-stack.yaml \
  --stack-name DevOpsAgentStack \
  --capabilities CAPABILITY_NAMED_IAM \
  --region <REGION>
```

### Langkah 3: Rekam output tumpukan
<a name="step-3-record-the-stack-outputs"></a>

Setelah penerapan selesai, jalankan perintah berikut untuk mengambil output stack. Catat nilai-nilai ini untuk digunakan nanti.

```
aws cloudformation describe-stacks \
  --stack-name DevOpsAgentStack \
  --query 'Stacks[0].Outputs' \
  --region <REGION>
```

Contoh berikut menunjukkan output yang diharapkan:

```
[
  {
    "OutputKey": "AgentSpaceId",
    "OutputValue": "abc123def456"
  },
  {
    "OutputKey": "AgentSpaceArn",
    "OutputValue": "arn:aws:aidevops:<REGION>:<ACCOUNT_ID>:agentspace/abc123def456"
  },
  {
    "OutputKey": "AgentSpaceRoleArn",
    "OutputValue": "arn:aws:iam::<ACCOUNT_ID>:role/DevOpsAgentRole-AgentSpace"
  },
  {
    "OutputKey": "OperatorRoleArn",
    "OutputValue": "arn:aws:iam::<ACCOUNT_ID>:role/DevOpsAgentRole-WebappAdmin"
  }
]
```

Jika Anda berencana untuk menyelesaikan Bagian 2, simpan `AgentSpaceArn` nilainya. Anda memerlukannya untuk mengonfigurasi peran lintas akun.

### Langkah 4: Verifikasi penyebaran
<a name="step-4-verify-the-deployment"></a>

Untuk memverifikasi bahwa ruang agen berhasil dibuat, jalankan perintah AWS CLI berikut:

```
aws devops-agent get-agent-space \
  --agent-space-id <AGENT_SPACE_ID> \
  --region <REGION>
```

Pada titik ini, ruang agen Anda digunakan dengan aplikasi operator diaktifkan dan akun pemantauan Anda terkait. Agen dapat memantau masalah di akun ini.

## Bagian 2 (Opsional): Tambahkan pemantauan lintas akun
<a name="part-2-optional-add-cross-account-monitoring"></a>

Di bagian ini, Anda memperpanjang pengaturan sehingga ruang agen Anda dapat memantau sumber daya di AWS akun kedua (akun layanan). Ini melibatkan dua tindakan:

1. Menyebarkan peran IAM di akun layanan yang mempercayai ruang agen.

1. Menambahkan AWS asosiasi sumber di akun pemantauan yang menunjuk ke akun layanan.

**Catatan:** Anda harus menyelesaikan Bagian 1 sebelum melanjutkan. Templat akun layanan memerlukan output tumpukan `AgentSpaceArn` dari Bagian 1.

### Langkah 1: Buat template akun layanan
<a name="step-1-create-the-service-account-template"></a>

Simpan template berikut sebagai`devops-agent-service-account.yaml`. Template ini membuat peran IAM lintas akun di akun sekunder.

```
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS DevOps Agent - Cross-account IAM role for secondary account monitoring

Parameters:
  MonitoringAccountId:
    Type: String
    Description: The 12-digit AWS account ID of the monitoring account
  AgentSpaceArn:
    Type: String
    Description: The ARN of the agent space from the monitoring account

Resources:
  # Cross-account IAM role trusted by the agent space
  DevOpsSecondaryAccountRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: DevOpsAgentRole-SecondaryAccount
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: aidevops.amazonaws.com
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                aws:SourceAccount: !Ref MonitoringAccountId
              ArnLike:
                aws:SourceArn: !Ref AgentSpaceArn
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AIDevOpsAgentAccessPolicy
      Policies:
        - PolicyName: AllowCreateServiceLinkedRoles
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: AllowCreateServiceLinkedRoles
                Effect: Allow
                Action:
                  - iam:CreateServiceLinkedRole
                Resource:
                  - !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/resource-explorer-2.amazonaws.com/AWSServiceRoleForResourceExplorer

Outputs:
  SecondaryAccountRoleArn:
    Description: The cross-account IAM role ARN
    Value: !GetAtt DevOpsSecondaryAccountRole.Arn
```

### Langkah 2: Menyebarkan tumpukan akun layanan
<a name="step-2-deploy-the-service-account-stack"></a>

Menggunakan kredensi untuk akun layanan, jalankan perintah berikut:

```
aws cloudformation deploy \
  --template-file devops-agent-service-account.yaml \
  --stack-name DevOpsAgentServiceAccountStack \
  --capabilities CAPABILITY_NAMED_IAM \
  --parameter-overrides \
    MonitoringAccountId=<MONITORING_ACCOUNT_ID> \
    AgentSpaceArn=<AGENT_SPACE_ARN> \
  --region <REGION>
```

### Langkah 3: Tambahkan AWS asosiasi sumber
<a name="step-3-add-the-source-aws-association"></a>

Beralih kembali ke akun pemantauan dan buat AWS asosiasi sumber. Anda dapat melakukan ini dengan membuat tumpukan terpisah atau dengan memperbarui template asli. Contoh berikut menggunakan template mandiri.

Simpan template berikut sebagai`devops-agent-source-association.yaml`:

```
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS DevOps Agent - Source AWS association for cross-account monitoring

Parameters:
  AgentSpaceId:
    Type: String
    Description: The agent space ID from the monitoring account stack
  ServiceAccountId:
    Type: String
    Description: The 12-digit AWS account ID of the service account
  ServiceAccountRoleArn:
    Type: String
    Description: The ARN of the DevOpsAgentRole-SecondaryAccount role in the service account

Resources:
  SourceAssociation:
    Type: AWS::DevOpsAgent::Association
    Properties:
      AgentSpaceId: !Ref AgentSpaceId
      ServiceId: aws
      Configuration:
        SourceAws:
          AccountId: !Ref ServiceAccountId
          AccountType: source
          AssumableRoleArn: !Ref ServiceAccountRoleArn

Outputs:
  SourceAssociationId:
    Description: The source association ID
    Value: !Ref SourceAssociation
```

Terapkan tumpukan asosiasi menggunakan kredensi akun pemantauan:

```
aws cloudformation deploy \
  --template-file devops-agent-source-association.yaml \
  --stack-name DevOpsAgentSourceAssociationStack \
  --parameter-overrides \
    AgentSpaceId=<AGENT_SPACE_ID> \
    ServiceAccountId=<SERVICE_ACCOUNT_ID> \
    ServiceAccountRoleArn=arn:aws:iam::<SERVICE_ACCOUNT_ID>:role/DevOpsAgentRole-SecondaryAccount \
  --region <REGION>
```

## Verifikasi
<a name="verification"></a>

Verifikasi pengaturan Anda dengan menjalankan perintah AWS CLI berikut:

```
# List your agent spaces
aws devops-agent list-agent-spaces \
  --region <REGION>

# Get details of a specific agent space
aws devops-agent get-agent-space \
  --agent-space-id <AGENT_SPACE_ID> \
  --region <REGION>

# List associations for an agent space
aws devops-agent list-associations \
  --agent-space-id <AGENT_SPACE_ID> \
  --region <REGION>
```

## Pemecahan masalah
<a name="troubleshooting"></a>

Bagian ini menjelaskan masalah umum dan cara mengatasinya.

**CloudFormation jenis sumber daya tidak ditemukan**
+ Verifikasi bahwa Anda menerapkan di file. [Wilayah yang Didukung](about-aws-devops-agent-supported-regions.md)
+ Konfirmasikan bahwa AWS CLI Anda dikonfigurasi dengan izin yang sesuai.

**Pembuatan peran IAM gagal**
+ Verifikasi bahwa kredenal penerapan Anda memiliki izin untuk membuat peran IAM dengan nama kustom (). `CAPABILITY_NAMED_IAM`
+ Periksa apakah ketentuan kebijakan kepercayaan sesuai dengan ID akun Anda.

**Penerapan lintas akun gagal**
+ Setiap tumpukan harus digunakan dengan kredensional untuk akun target. Gunakan `--profile` bendera untuk menentukan profil AWS CLI yang benar.
+ Verifikasi bahwa `AgentSpaceArn` parameter cocok dengan ARN yang tepat dari output tumpukan Bagian 1.

**Penundaan propagasi IAM**
+ Perubahan peran IAM dapat memakan waktu beberapa menit untuk disebarkan. Jika pembuatan ruang agen gagal segera setelah pembuatan peran, tunggu beberapa menit dan gunakan kembali.

## Pembersihan
<a name="cleanup"></a>

Untuk menghapus semua sumber daya, hapus tumpukan dalam urutan terbalik.

**Peringatan:** Tindakan ini secara permanen menghapus ruang agen Anda dan semua data terkait. Tindakan ini tidak dapat dibatalkan. Pastikan Anda telah mencadangkan informasi penting apa pun sebelum melanjutkan.

Jalankan perintah berikut untuk menghapus tumpukan:

```
# If you deployed the source association stack, delete it first
aws cloudformation delete-stack \
  --stack-name DevOpsAgentSourceAssociationStack \
  --region <REGION>

aws cloudformation wait stack-delete-complete \
  --stack-name DevOpsAgentSourceAssociationStack \
  --region <REGION>

# If you deployed the service account stack, delete it next (using service account credentials)
aws cloudformation delete-stack \
  --stack-name DevOpsAgentServiceAccountStack \
  --region <REGION>

aws cloudformation wait stack-delete-complete \
  --stack-name DevOpsAgentServiceAccountStack \
  --region <REGION>

# Delete the main stack last
aws cloudformation delete-stack \
  --stack-name DevOpsAgentStack \
  --region <REGION>
```

## Langkah selanjutnya
<a name="next-steps"></a>

Setelah Anda menerapkan AWS DevOps Agen Anda dengan menggunakan AWS CloudFormation:
+ Untuk menghubungkan integrasi tambahan, lihat[Mengkonfigurasi kemampuan untuk Agen AWS DevOps](configuring-capabilities-for-aws-devops-agent.md).
+ Untuk mempelajari keterampilan dan kemampuan agen, lihat[DevOps Keterampilan Agen](about-aws-devops-agent-devops-agent-skills.md).
+ Untuk memahami aplikasi web operator, lihat[Apa itu Aplikasi Web DevOps Agen?](about-aws-devops-agent-what-is-a-devops-agent-web-app.md).