Add a security group to an existing security group

I want to add a security group from AWS Beanstalk to a security group for the database. What is the best way to achieve this with Terraform?

I’m not 100% sure what you mean, but Terraform is just describing whatever you’d otherwise do using the CLI or Console. So if you are meaning having a SG rule that references another SG then you just need to add the rule resource with that configuration in your Terraform code.