DEV Community

Cover image for Debuggar os projetos CDK em Python no VS Code
João Vitor Reis Martins
João Vitor Reis Martins

Posted on

Debuggar os projetos CDK em Python no VS Code

O CDK, ou Cloud Development Kit, é uma estrutura de código aberto que simplifica o trabalho com serviços de nuvem. Ele permite que os desenvolvedores definam recursos de nuvem usando linguagens de programação conhecidas, incluindo Python. A depuração desses projetos é essencial para garantir que eles funcionem conforme esperado, para identificar e corrigir erros e melhorar a eficiência do código.

Através do Debug, é posível realizar análises de pacotes, variáveis, funções, e todas as entradas e saídas do código. Com esse recurso, fica fácil localizar bugs ou gargalos dentro da aplicação.

Para realizar o Debug do CDK em Python, primeiro é importante criar o ambiente venv, instalar os pacotes necessários, setar as variáveis de ambiente, e depois escolher o interpretador correto:

  1. Crie o ambiente virtual Python (venv) usando o comando python3 -m venv .venv
  2. Ative o ambiente venv com source .venv/bin/activate No Windows: .venv\Scripts\activate
  3. Instale os pacotes necessários com pip3 install -r requirements.txt
  4. Crie o arquivo launch.json, clicando no ícone de debug, em seguida em “create a launch.json file” Image description
  5. (Opcional) Caso o arquivo já tenha sido criado, aperte Ctrl+Shift+P e pesquise por launch.json
  6. Preencha o arquivo launch.json, alterando as configurações necessárias:
{
      "version": "0.2.0",
      "configurations": [

        {
          "name": "Python: Attach using Process Id",
          "type": "debugpy",
          "request": "attach",
          "processId": "${command:pickProcess}",
          "justMyCode": true
        },
        {
          "name": "Python: Current File",
          "type": "debugpy",
          "request": "launch",
          "program": "${file}",
          "console": "integratedTerminal",
          "justMyCode": true,
          "env": {
            "Var1": "TestValue",
                    "Var2": "TestValue"
          }
        }
      ]
    }
Enter fullscreen mode Exit fullscreen mode
  1. Digite Ctrl+Shift+P e procure Interpreter
  2. Selecione o interpretador python.exe que está dentro da .venv No windows, \Scripts\python.exe
  3. Abra o arquivo inicial do código, coloque um stop point, e clique na opção Image description
  4. Troubleshooting: Caso haja algum problema na hora de rodar os comandos do cdk, como por exemplo comando de synth, deploy, não estiver localizando o pacote. Tente executar tanto com python ou python3, o interpretador pode estar errado.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay